[Tutor] How does it function

Steve Nelson sanelson at gmail.com
Wed Mar 29 16:26:16 CEST 2006


On 3/29/06, Kaushal Shriyan <kaushalshriyan at gmail.com> wrote:
> Hi ALL
>
> Just wanted to know the detailed explanation about the below statement
>
> if __name__ == "__main__":

Simple answer - any python program you write is effectively a
'module'.  Modules have an attribute __name__.  If you've imported the
module from elsewhere, the __name__ is set to the name of the module,
otherwise it is __name__.

This means that you can write a test that says:If the code we're
trying to run is the main program, go ahead and start running the
functions we need.

You can read more about it here:

http://swaroopch.info/text/Byte_of_Python:Modules

and also here:

http://diveintopython.org/getting_to_know_python/testing_modules.html

More detailed info here:

http://www.python.org/doc/current/ref/import.html

S.


More information about the Tutor mailing list