if __name__ == '__main__':

Michael Wieher michael.wieher at gmail.com
Thu Mar 20 17:34:54 CET 2008


Well, consider this:

you have a file named <mymodule>.py, built like this:
~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/python

def <function>:
    ....
    return <data>

if __name__=="__main__":
    print "Unit test"
else:
    pass #module imported by another module/script
~~~~~~~~~~~~~~~~~~~~~~

If you type, on command line, >python ./<mymodule>.py
you will see "Unit test" printed to the screen.

if, however you are in another python file and type "import <mymodule>" the
code will, instead, "pass" and nothing will occur.

I hope this helps =)


2008/3/20, Simon Brunning <simon at brunningonline.net>:
>
> On Thu, Mar 20, 2008 at 4:12 PM, Bhagwat Kolde <bbkolde at gmail.com> wrote:
> > Hi,
> > I am new to the python and not getting meaning of following line,
> >
> > if __name__ == '__main__':
> >       main()
>
> <
> http://www.python.org/doc/faq/programming/#how-do-i-find-the-current-module-name
> >
>
> --
> Cheers,
> Simon B.
> simon at brunningonline.net
> http://www.brunningonline.net/simon/blog/
> GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080320/c0ce147f/attachment.html>


More information about the Python-list mailing list