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


Groups > comp.lang.python > #80675 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2014-11-13 19:58 +1100
Last post2014-11-14 00:40 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromChris Angelico <rosuav@gmail.com>
Date2014-11-13 19:58 +1100
SubjectRe: How about some syntactic sugar for " __name__ == '__main__' "?
Message-ID<mailman.15769.1415869145.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#80683

FromRoy Smith <roy@panix.com>
Date2014-11-13 08:33 -0500
Message-ID<roy-C2126E.08323413112014@news.panix.com>
In reply to#80675
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.

[toc] | [prev] | [next] | [standalone]


#80685

FromChris Angelico <rosuav@gmail.com>
Date2014-11-14 00:40 +1100
Message-ID<mailman.15775.1415886018.18130.python-list@python.org>
In reply to#80683
On Fri, Nov 14, 2014 at 12:33 AM, Roy Smith <roy@panix.com> wrote:
> ... you also get to not worry
> about what order things are defined.

That's only as regards the interpreter, though. My point has nothing
to do with the order the interpreter sees things, it's all about how
they're laid out for humans to read. For that, I prefer to have
definitions before use.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web