Total newbie question: Best practice

Neil Cerutti neilc at norwich.edu
Tue Nov 29 16:12:48 EST 2011


On 2011-11-29, Arnaud Delobelle <arnodel at gmail.com> wrote:
> As for the main() function, I don't think it is standard
> practice in Python.  There is no requirement to have a main()
> function.  You can use the idiom:

I don't start off with a main function, but if my script gets
long and complicated, or if global names have proliferated and
have become confusing, I'll refactor the whole thing into
functions, including a "main". With most globals moved into
main's namespace, calling subroutines from main forces me to
define the context that's actually necessary for each part of the
program.

The resultant refactored programs are much easier to test, read
and maintain.

TLDR: "Called-only-once" functions like main are useful as
documentation, hooks for testing, and for unraveling a snarl of
global variables.

-- 
Neil Cerutti



More information about the Python-list mailing list