[Chicago] __name__ == "__main__"

Martin Maney maney at two14.net
Sat Oct 27 05:15:44 CEST 2007

On Fri, Oct 26, 2007 at 10:46:06AM -0500, Michael Tobis wrote:
> def invoke_lots_of_stuff():
>    print "I represent a lot of very clever objects and functions"
> def main():
>    invoke_lots_of_stuff()
> if __name__ == "__main__":
>    main()

> What's the purpose of the extra layer provided by the main() function?

If it's just a single call as sketched above it does seem kind of
pointless.  But the general pattern would allow you to accrete a bunch
of separate little widgets, each written in that fashion, into a single
master application (think "svn <subcommand-name> ..." for example) with
really minimal effort and maximum flexibility even when main is more
than a wrapper around one function call.

...though in that case you should be passing any arguments to main as
explicit parameters rather than fishing them out of sys.argv, so maybe
there's something else going on?  Hmmm...

Self-pity can make one weep, as can onions.  -- Fodor

More information about the Chicago mailing list