0

Can someone explain __main__?

The solo learn explanation of __main__ went straight over my head and I did not understand it at all. I know I can google away but I am hoping someone can point to a clear explanation that they deemed clear. Too much unclear "explanation"...

5th Oct 2020, 6:07 AM
Nassim Abed
Nassim Abed - avatar
6 Respuestas
+ 6
The comment section has nice explanations, check it out, always. Your idea in below question is wrong: https://www.sololearn.com/Discuss/170057/?ref=app I'll try to explain it with my own words: We have a file "A.py" that looks like this: print(__name__ == "__main__") If you run that file you will get True as output. But if you run another file "B.py" that looks this way: import A #False, A now is a module print(__name__ == "__main__") #True, the current file (B) is the main one.
5th Oct 2020, 6:38 AM
Kevin ★
+ 4
I think that's well explained in SoloLearn's tutorial: [...if the Python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.]
5th Oct 2020, 6:58 AM
Kevin ★
+ 2
Thanks Kevin. So __name__ is not a variable that changes and takes a value other than __main__ when imported as a module. It is either equal to "__main__" or not equal to "__main__" in which case it is not having some other value. Correct?
5th Oct 2020, 6:43 AM
Nassim Abed
Nassim Abed - avatar
+ 1
So much is clear. Then why if if do import re followed by print(__name__) I get "__main__"??
5th Oct 2020, 6:31 AM
Nassim Abed
Nassim Abed - avatar
0
Yes but testing tells a different story.
6th Oct 2020, 12:57 PM
Nassim Abed
Nassim Abed - avatar