about if __name == '__main__':

I

Ian Kelly

hello group

i have one question about this

if __name == '__main__':

First, it should be:

if __name__ == '__main__':
is it same as other languages like[c,c++]  main function. because of i
google and read faqs
and also " http://docs.python.org/faq/programming#how-do-i-find-the-current-module-name"
this and i am confused.

No, that is not a main function. It's not even a function. When
Python runs a script, it loads that script as a module, sets its name
to be __main__, and then executes the entire module, starting from the
top as normal. What that if statement defines is an ordinary branch
that is only executed if the current module is the main module, as
opposed to having been imported from some other module. Normally this
will be at the end of the file so that all the definitions in the file
will have already been executed.

The usual idiom for this is:

def main(argv):
# parse arguments and begin program logic...
pass

if __name__ == '__main__':
import sys
main(sys.argv)

This is also frequently used for unit testing of library modules, so
that the module can be tested just by running it.

# define library classes and functions here

if __name__ == '__main__':
# perform unit tests

Cheers,
Ian
 
W

woooee

Two main routines, __main__ and main(), 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.
 
T

Terry Reedy

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.
 
I

Ian Kelly

Two main routines, __main__ and main(), 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.

Not a problem if you write them one after another. Besides which, any
decent editor will have a command to go directly from the function
call to the function definition, so I really don't see this as a
problem.
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.

Or if your main "routine" has variables, and you don't particularly
want them polluting the module's global namespace.
In which case, use a name that is descriptive, not "main".

"main" is descriptive in that it clearly designates the entry point of
the script to anybody accustomed to such terminology. But there is
certainly no reason it needs to be "main", and in fact I only used
that for the example. In an actual script I would use whatever seems
appropriate.
 
C

Cameron Simpson

| Two main routines, __main__ and main(), 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.

Firstly, as Terry remarked, __main__ is a name, not a function.

Secondly, "search the program for the statements that should have been at the
bottom of the program" isn't how I see it. If I have a module I expect
to be usable from the command line easily it looks like this:

def main(argv):
... command line program logic ...
return exit_code

... all the other module contents ...

if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

That way the top level command line program logic is at the top of the file
where it is easy to find, not buried in the middle or at the bottom.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top