What is __main__ ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is __main__ ??

14th Jan 2017, 3:37 PM
chaurasiya abhimanyu harilal
2 Answers
+ 3
Before programs are executed, special variables are defined. Take into account, you're running your program from a source file and that source file is like your main program. A special variable that's called __name__ will have the value of '__main__'. If you're importing the module to like another module __name__ will have the value of the module's name instead of '__main__'. All in all, it's not too hard of a concept. If you're confused I would strongly recommend for you to search it up. You might learn something new.
15th Jan 2017, 6:30 AM
Don
Don - avatar
0
It seems to be one of those "too simple" situations. Let me see if I understand this - please correct me if I'm wrong: There is a default __name__ that has the value "__main__". This default is only changed, behind the scenes, when the code includes an import command, in which case the value of __name__ becomes that of the imported command. So if my understanding is correct, then this code below will print "__main__" and "re" on two lines: print(__name__) import re print(__name__) But it doesn't: it prints "__main__" twice. Likewise if I change import re to from sys import argv. I still get "__main__" printed twice. What am I missing? PS I'm the guy who finds the iPhone too simple to be user-friendly and struggle with it.
5th Oct 2020, 6:15 AM
Nassim Abed
Nassim Abed - avatar