I didn't understand __main__ concept? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I didn't understand __main__ concept?

Can anyone say where can I use it

27th Dec 2016, 3:21 AM
GOLI NAGA MANIKANTA SURYA RAJESH
GOLI NAGA MANIKANTA SURYA RAJESH - avatar
4 Answers
+ 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.
27th Dec 2016, 3:25 PM
Shubham Menkudle
Shubham Menkudle - avatar
+ 1
Dude just go thru tutorial again
27th Dec 2016, 5:44 AM
hackingmaster
hackingmaster  - avatar
+ 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)
27th Dec 2016, 7:40 AM
Marcos Perico Lospalotes
Marcos Perico Lospalotes - avatar
0
Thank you all
27th Dec 2016, 3:31 PM
GOLI NAGA MANIKANTA SURYA RAJESH
GOLI NAGA MANIKANTA SURYA RAJESH - avatar