[Chicago] __name__ == "__main__"

Ian Bicking ianb at colorstudy.com
Fri Oct 26 18:01:41 CEST 2007

Michael Tobis wrote:
> Hi, Ian! Thanks for trying.
> Unfortunately, I still don't actually see how main() is better than
> just putting what you have in main() inline under the __name__
> conditional...

Ah, wasn't sure what you meant.  Well... you can do things like "return" 
in main(), that you can't do at the top level.  Sometimes I find myself 
putting a try:except: in the __main__ block, around main().  I think 
it's a habit that you develop after having written lots of scripts, and 
refactoring them, and moving stuff out of __main__ enough times that you 
just do it automatically from the start.

Also, sometimes it's actually handy to get at the command-line interface.

Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org

More information about the Chicago mailing list