[Chicago] __name__ == "__main__"

Chris McAvoy chris.mcavoy at gmail.com
Fri Oct 26 17:55:14 CEST 2007


I'm guilty of not doing this often enough.  Sometimes I treat Python
like a *gasp* scripting language.

I make no apologies.

Chris

On 10/26/07, Ian Bicking <ianb at colorstudy.com> wrote:
> Michael Tobis wrote:
> > I agree with everything Kumar says. It could be prettier, but it's
> > extremely useful to put the
> > __name__ == "__main__" around code in your file that doesn't define
> > objects or functions.
> >
> > Now that it's come up, though. what I never quite understood was the
> > extra level of indirection that many people including Guido favor.
> >
> > ###
> > 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?
> >
> > Also, yeah, there's a little bit of a Python activity in Chicago...
> > (Miss y'all; looking forward to seeing you again in March.)
>
> Usually main() handles the command-line interface, which specifically
> parses a list of strings, and maybe calls sys.exit.
> invoke_lots_of_stuff would generally have a Python interface, taking
> keyword arguments and stuff that isn't a string.  So when you import the
> code and use it from Python, you'd just use invoke_lots_of_stuff() and
> not main().
>
>
> --
> Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list