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

Terry Reedy tjreedy at
Thu Nov 13 01:35:59 CET 2014

On 11/12/2014 6:26 PM, Chris Angelico wrote:
> On Thu, Nov 13, 2014 at 10:19 AM, Terry Reedy <tjreedy at> wrote:
>> Functions have an implicit 'return None' at the end (which, in CPython,
>> become an explicit pair of bytecodes, even when the function already ends
>> with return something'.  The simplest proposal is that modules have an
>> implicit "if __name__ == '__main__': main()" at the end.  I think this would
>> not have to be added to the bytecode.
>> This magical invocation mimics C and some other languages, and I think it
>> works well.
> Yes, but it conflicts with the existing and common usage of having
> that explicitly in the code.

Yeh, calling main twice could be a problem.

> Safer - and more in line with the way
> other such functions are written - would be a dunder function:
> if __name__ == '__main__': __main__()

I presume you mean that calling __main__ implicitly would be both 
consistent and safer.  No code should be using that now.

Terry Jan Reedy

More information about the Python-list mailing list