[Chicago] __name__ == "__main__"

Dave Niesman dave.niesman at ieee.org
Fri Oct 26 18:02:36 CEST 2007


How about these scenarios:

You are developing a relative big main with lots of functions, and you want
to test it function by function.

You are developing a library module and it doesn't really have a command
line function, this is a great time to run the tests.

if __name__ == "__main__"
     runTheTests()


On 10/26/07, Michael Tobis <mtobis at gmail.com> 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...
>
> mt
>
> 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
> >
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20071026/56ef2942/attachment-0001.htm 


More information about the Chicago mailing list