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


Groups > comp.lang.python > #12358

Re: about if __name == '__main__':

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'received:edu.au': 0.07; 'terry': 0.07; '__name__': 0.09; 'executed': 0.09; 'def': 0.15; "'__main__':": 0.16; 'bottom.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'program"': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'command': 0.24; 'expect': 0.25; 'import': 0.28; 'order.': 0.29; 'looks': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; 'logic': 0.30; 'usable': 0.30; 'usual': 0.31; 'anyone': 0.32; "isn't": 0.33; 'it.': 0.33; '...': 0.34; 'header :User-Agent:1': 0.34; 'function.': 0.34; 'charset:us-ascii': 0.36; 'file': 0.36; 'received:au': 0.36; 'statements': 0.37; 'two': 0.37; 'should': 0.38; 'subject:: ': 0.39; 'called': 0.40; 'where': 0.40; 'easily': 0.61; 'bottom': 0.64; 'received:202': 0.66; 'cameron': 0.67; 'find,': 0.84; 'isolated': 0.84
Date Mon, 29 Aug 2011 09:59:43 +1000
From Cameron Simpson <cs@zip.com.au>
To woooee <woooee@gmail.com>
Subject Re: about if __name == '__main__':
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.520.1314575993.27778.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314575993 news.xs4all.nl 2475 [2001:888:2000:d::a6]:38894
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12358

Show key headers only | View raw


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.

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