DEV Community

Discussion on: What's the use of if __name__ == '__main__': in Python?

Collapse
 
thefern profile image
Fernando B 🚀

The way I think about it, is as a main method like in c++. Unless you have another 'main' on another module and import it somewhere else in that case main will not execute. So is safe to assume that you should only have one main entry point in your whole program.

I think the best way to explain it to a beginner would be to write some script without using main, and another script with using main running it and seeing the execution results in the console. At least that's how I learn stuff lol.

Here's an overcomplicated answer on SO.

stackoverflow.com/q/419163/1013828