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

Chris Angelico rosuav at gmail.com
Thu Nov 13 09:04:22 CET 2014


On Thu, Nov 13, 2014 at 6:23 PM, Cameron Simpson <cs at zip.com.au> wrote:
> 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.

Hmm, I go the other way. As much as possible, I prefer functions to
call what's above them, not what's below them - so the main function
would always be at the end of the file. The very top of the file
should have comments/docstrings etc, then imports, then pure utility
functions that don't call on anything else, then "guts" functions, and
finally routines that are called externally but never internally (like
main). That way, if you're wondering at what some name means, you go
to the top of the file and find the first occurrence; that'll usually
be its definition. Most people already do this with their imports,
putting them all at the top - I like to go further and make it unusual
for this not to be the case. (Of course, the nature of live projects
and constant editing is that there will be violations of the
principle, and I don't code-churn just to fix it. But that's still the
ideal.)

ChrisA



More information about the Python-list mailing list