if __name__ == "__main__"

Kevin Cazabon kevin at cazabon.com
Mon Dec 17 18:09:49 EST 2001


THis is commonly used to allow a script to determine if this module
was "executed" or "imported" script another script.

In the namespace of the module that was the one actually executed,
__name__ will be "__main__".  In all others, it will be something else
(hmmm... what will it be, never thought about it?).

So, you can use "if __name__ == '__main__': do_this()" to build in a
self-test for that particular module, or use it to allow the module to
be used on its own, or to provide functions to other scripts.

Kevin.

crennert at pbmplus.com (Chris Rennert) wrote in message news:<afd0beb9.0112171149.4adaab5f at posting.google.com>...
> Hello,
> 
> 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?  
> Any help would be greatly appreciated.



More information about the Python-list mailing list