if __name__ == "__main__"
Martin von Loewis
loewis at informatik.hu-berlin.de
Fri Dec 21 17:38:46 CET 2001
crennert at pbmplus.com (Chris Rennert) writes:
> Could someone please fill me in on how the if __name__ == "__main__
> thing works. I am reading through some tutorials and I never really
> get a clear definitive answer on that.
> Is this like main() in C?
No. __name__, on module level, is the name of the module. So if you
would modify urllib.py to contain
then "import urllib" would print
Now, the file that you pass on the command line of the interpreter
does not have a module name (because it really isn't a module), so
__name__ is set to the string "__main__" inside that code. That allows
you to write a file that is used both as a module and as a program.
More information about the Python-list