DEV Community

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

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Unlike the C family of languages, where program execution starts with the main() function... Python allows code that isn't in a function or class at all. If you import a module containing such code, it will be executed. If this isn't desirable, for example because it's intended to only get used when the module is run directly, putting the code behind a if __name__ == '__main__': guard does exactly that!