+ 1
I didn't understand __main__ concept?
Can anyone say where can I use it
4 ответов
+ 2
The __name__ variable points to the namespace wherever the Python interpreter happens to be at the moment. Inside an imported module, it's the name of that module. But inside the primary module (or an interactive Python session) you are running everything from its "__main__".
if __name__=="__main__":
One of the reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.
+ 1
Dude just go thru tutorial again
+ 1
I think it is the name under which run when they are executed in the interpreter. If you want a program to perform, say 2+2, only when you run it in the interpreter, your first line program should be like
if name=="__main__":
print(2+2)
0
Thank you all