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


Groups > comp.lang.python > #4588

Re: vertical ordering of functions

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news.astraweb.com!border5.newsrouter.astraweb.com!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: vertical ordering of functions
References <mailman.1127.1304460530.9059.python-list@python.org>
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
Date Wed, 04 May 2011 09:58:45 +1000
Message-ID <87sjsvfia2.fsf@benfinney.id.au> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)
Cancel-Lock sha1:bY+Ja1LFpVBgIJfcPH/ZFMgsxvI=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 32
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host e0fd4828.news.astraweb.com
X-Trace DXC=Zj@bXb:>INeJRZZkg^aP1jL?0kYOcDh@jN7:H2`MmAUcoCS<dd:ZYaj]G;2>V^?kWcbEW9A[5UK?eNZ[SL`C\KgcNQV8ljiLBlj
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4588

Show key headers only | View raw


Jabba Laci <jabba.laci@gmail.com> writes:

> Is there a convention for this? Should main() be at the top and called
> function below?

No, it's Python convention for both of those to be at the end of the
module.

I follow the convention described by Guido van Rossum in
<URL:http://www.artima.com/weblogs/viewpost.jsp?thread=4829>.

Essentially, the mainline code is all in a top-level function, which
accepts command-line arguments in its ‘argv’ parameter, catches any
‘SystemExit’ exception, and returns the exit code for the program.

The ‘if __name__ == "__main__"’ section does nothing but call that
function, passing the ‘sys.argv’ value, then exit with the return value.

This makes the whole behaviour of the program available for calling as a
function, while the program works as expected when invoked as a program.

Commonly I will name the function ‘__main__’, but this is an artefact of
my previously-held vain hope that this idiom would become special to
Python and called automatically without the ‘if __name__ …’ hack. Guido,
and most others I've seen use this idiom, simply name the function
‘main’.

-- 
 \       “An idea isn't responsible for the people who believe in it.” |
  `\                                      —Donald Robert Perry Marquis |
_o__)                                                                  |
Ben Finney

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


Thread

vertical ordering of functions Jabba Laci <jabba.laci@gmail.com> - 2011-05-03 18:08 -0400
  Re: vertical ordering of functions Ben Finney <ben+python@benfinney.id.au> - 2011-05-04 09:58 +1000
    Re: vertical ordering of functions John Bokma <john@castleamber.com> - 2011-05-03 19:44 -0500
  Re: vertical ordering of functions Roy Smith <roy@panix.com> - 2011-05-03 20:27 -0400
  Re: vertical ordering of functions Grant Edwards <invalid@invalid.invalid> - 2011-05-04 01:09 +0000
  Re: vertical ordering of functions John Roth <johnroth1@gmail.com> - 2011-05-04 04:14 -0700
  Re: vertical ordering of functions Hans Georg Schaathun <hg@schaathun.net> - 2011-05-10 19:31 +0100

csiph-web