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


Groups > comp.lang.python > #12357

Re: about if __name == '__main__':

References <c0025c7f-8fb1-4be6-a41b-6227aa388a53@h23g2000pra.googlegroups.com> <mailman.509.1314550323.27778.python-list@python.org> <5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-08-28 17:55 -0600
Subject Re: about if __name == '__main__':
Newsgroups comp.lang.python
Message-ID <mailman.519.1314575771.27778.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web