about if __name == '__main__':

Terry Reedy tjreedy at udel.edu
Sun Aug 28 17:35:24 EDT 2011


On 8/28/2011 2:56 PM, woooee wrote:
> Two main routines, __main__ and main(),

'__main__' in not a routine, it is the name of the initial module.

> is not the usual or the common
> way to do it.  It is confusing and anyone looking at the end of the
> program for statements executed when the program is called will find
> an isolated call to main(), and then have to search the program for
> the statements that should have been at the bottom of the program.
> The only reason to use such a technique in Python is if you want to
> call the function if the program is run from the command line, and
> also call the same function if the program is imported from another.
> In which case, use a name that is descriptive, not "main".  And be
> careful of anyone that gives you programming advice.  Research these
> things for yourself.

As far as I know, all the Lib/test/test_xxx.py file have a test_main 
function, so one can write (in IDLE, for instance)
from test.test_xxx import test_main as f; f()
and run that test. Very handy.

-- 
Terry Jan Reedy




More information about the Python-list mailing list