[Python-ideas] Just __main__

Serhiy Storchaka storchaka at gmail.com
Mon Jun 18 19:12:39 CEST 2012


On 18.06.12 19:17, Ethan Furman wrote:
> anatoly techtonik wrote:
>> How about global __main__ as a boolean?
>>
>> __name__ == '__main__' as a mark of entrypoint module is coherent and
>> logical, but awkward to type and requires explicit explaination for
>> newcomers even with prior background in other langauges.
>
> So instead of:
>
> if __name__ == '__main__':
> ...
>
> you would have:
>
> if __main__:
> ...
>
> ?

No, it is much easier.

   import sys
   if __main__ if sys.version_info >= (3, 9) else __name__ == '__main__':
       ...

or

   try:
       __main__
   except NameError:
       __main__ = __name__ == '__main__'
   if __main__:
       ...




More information about the Python-ideas mailing list