Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80683

Re: How about some syntactic sugar for " __name__ == '__main__' "?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: How about some syntactic sugar for " __name__ == '__main__' "?
Date 2014-11-13 08:33 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-C2126E.08323413112014@news.panix.com> (permalink)
References <CAPTjJmr9BbFQzEYzWgW+2Y_cB-kSaXZZQ0nK4Mr=TJf7i0dZXQ@mail.gmail.com> <20141113084725.GA97979@cskk.homeip.net> <mailman.15769.1415869145.18130.python-list@python.org>

Show all headers | View raw


In article <mailman.15769.1415869145.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Thu, Nov 13, 2014 at 7:47 PM, Cameron Simpson <cs@zip.com.au> wrote:
> > My view is that if there's a main (i.e. the module implements a small app
> > all on its own, however tiny), then the main program logic should come
> > first. The details follow later.
> 
> Ah, I see. Makes sense. It's kinda like an executable docstring. Still
> not my preferred style, but makes its own sense.
> 
> ChrisA

I generally define a main() routine up near the top, then put

if __name__ == '__main__':
    main()

at the bottom.  That gives you the best of both worlds; you get to see 
the main() code up front in the file, but you also get to not worry 
about what order things are defined.

It also means you can import your module and call its main() directly.  
That's useful for testing, but occasionally for other purposes too.  You 
can't do that with the (detestable) style of putting a whole bunch of 
code in the body of the "if" statement.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-13 19:58 +1100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Roy Smith <roy@panix.com> - 2014-11-13 08:33 -0500
    Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-14 00:40 +1100

csiph-web