Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12343 > unrolled thread

about if __name == '__main__':

Started byAmit Jaluf <amitjaluf@gmail.com>
First post2011-08-28 08:34 -0700
Last post2011-08-28 20:42 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  about  if __name == '__main__': Amit Jaluf <amitjaluf@gmail.com> - 2011-08-28 08:34 -0700
    Re: about if __name == '__main__': Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-28 10:51 -0600
      Re: about if __name == '__main__': woooee <woooee@gmail.com> - 2011-08-28 11:56 -0700
        Re: about if __name == '__main__': Terry Reedy <tjreedy@udel.edu> - 2011-08-28 17:35 -0400
        Re: about if __name == '__main__': Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-28 17:55 -0600
        Re: about if __name == '__main__': Cameron Simpson <cs@zip.com.au> - 2011-08-29 09:59 +1000
      Re: about if __name == '__main__': Amit Jaluf <amitjaluf@gmail.com> - 2011-08-28 20:42 -0700

#12343 — about if __name == '__main__':

FromAmit Jaluf <amitjaluf@gmail.com>
Date2011-08-28 08:34 -0700
Subjectabout if __name == '__main__':
Message-ID<c0025c7f-8fb1-4be6-a41b-6227aa388a53@h23g2000pra.googlegroups.com>
hello group

i have one question about this

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.

thanks

[toc] | [next] | [standalone]


#12346 — Re: about if __name == '__main__':

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-08-28 10:51 -0600
SubjectRe: about if __name == '__main__':
Message-ID<mailman.509.1314550323.27778.python-list@python.org>
In reply to#12343
On Sun, Aug 28, 2011 at 9:34 AM, Amit Jaluf <amitjaluf@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#12348 — Re: about if __name == '__main__':

Fromwoooee <woooee@gmail.com>
Date2011-08-28 11:56 -0700
SubjectRe: about if __name == '__main__':
Message-ID<5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
In reply to#12346
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.

[toc] | [prev] | [next] | [standalone]


#12355 — Re: about if __name == '__main__':

FromTerry Reedy <tjreedy@udel.edu>
Date2011-08-28 17:35 -0400
SubjectRe: about if __name == '__main__':
Message-ID<mailman.518.1314567382.27778.python-list@python.org>
In reply to#12348
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

[toc] | [prev] | [next] | [standalone]


#12357 — Re: about if __name == '__main__':

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-08-28 17:55 -0600
SubjectRe: about if __name == '__main__':
Message-ID<mailman.519.1314575771.27778.python-list@python.org>
In reply to#12348
On Sun, Aug 28, 2011 at 12:56 PM, woooee <woooee@gmail.com> wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#12358 — Re: about if __name == '__main__':

FromCameron Simpson <cs@zip.com.au>
Date2011-08-29 09:59 +1000
SubjectRe: about if __name == '__main__':
Message-ID<mailman.520.1314575993.27778.python-list@python.org>
In reply to#12348
On 28Aug2011 11:56, woooee <woooee@gmail.com> wrote:
| 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,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Anarchy is not lack of order. Anarchy is lack of ORDERS.

[toc] | [prev] | [next] | [standalone]


#12384 — Re: about if __name == '__main__':

FromAmit Jaluf <amitjaluf@gmail.com>
Date2011-08-28 20:42 -0700
SubjectRe: about if __name == '__main__':
Message-ID<a45d6070-71ac-4aae-9061-69390ff1819f@e20g2000prn.googlegroups.com>
In reply to#12346
On Aug 28, 12:51 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Sun, Aug 28, 2011 at 9:34 AM, Amit Jaluf <amitja...@gmail.com> wrote:
> > hello group
>
> > i have one question about this
>
> > if __name == '__main__':

sorry dear for this
and thanks all of you for this................

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web