Huh?!?!? What is it that I'm seeing here?

Tim Peters tim.one at comcast.net
Sun Sep 14 19:32:34 EDT 2003


[Don Bruder, wants to know what this does ...]
> if __name__ == '__main__':
>   run(argv[1:])

__name__ is the name of the module, usually derived from the file name after
an import statement.  Python makes up the name '__main__' if a module is run
directly from a command line, though.  So the code in the body of the 'if'
is skipped if the module is imported by some other module, while the code in
the body of the 'if' is executed if the module is run directly.  The idiom
is often used to arrange to run self-test code when the module is run
directly, or to provide a handy cmdline utility related to the module's
purpose.  There's nothing similar in C or C++, as the construct is
triggering off how the module got loaded.






More information about the Python-list mailing list