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


Groups > comp.lang.python > #80670

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

Date 2014-11-13 18:23 +1100
From Cameron Simpson <cs@zip.com.au>
Subject Re: How about some syntactic sugar for " __name__ == '__main__' "?
References <CALwzid=i6vKUnQU1pfO3qs8H5u+_J4ff_XdkQUxt4ajN5JNYfQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.15765.1415864451.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12Nov2014 14:51, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>On Wed, Nov 12, 2014 at 2:33 PM, Chris Kaynor <ckaynor@zindagigames.com> wrote:
>> A decorator is an interesting idea, and should be easy to implement (only
>> lightly tested): [...]
>> Just decorate the "main" function of the script with that, and it will be
>> automatically called when ran as a script, but not when imported as a
>> module.
>
>This calls it at the wrong time, though. [...]
>This would call the main function at the time it's defined, when other
>things in the main script may not have been defined yet. One could
>place the main function last, but it would be preferable not to be
>forced.

Indeed. This aspect is a deal breaker for me; I'd never use it.

I make a point of putting the module's main function right up the top, 
immediately after the imports and any "constants" (let's not dither over that 
term). I _want_ the main function to in the reader's face when they visit the 
module code. All the cogs come later.

And lots of my modules have "main" functions. Terribly useful.

Cheers,
Cameron Simpson <cs@zip.com.au>

We're in the business of putting goo on a substrate.
- overhead by WIRED at the Intelligent Printing conference Oct2006

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


Thread

Re: How about some syntactic sugar for " __name__ == '__main__' "? Cameron Simpson <cs@zip.com.au> - 2014-11-13 18:23 +1100

csiph-web