newb: What is the purpose of if __name__ == "__main__":
Soni Bergraj
sonibergraj at youjoy.org
Wed Dec 6 22:12:43 EST 2006
johnny wrote:
> What is the purpose of
> if __name__ == "__main__":
>
> If you have a module, does it get called automatically?
>
It is a common pattern to make one and the same source file usable as a
program and as a module. When you import some python module its
__name__ is never "__main__". When you run a python script __name__ is
always "__main__". This way you can control what shall happen when you
run the source file as a standalone program.
To put it simple: Everything in the `if __name__ == "__main__":` block
is only executed when you run the source file as a program.
Hope that helps,
--
Soni Bergraj
http://www.YouJoy.org/
More information about the Python-list
mailing list