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

Ian Kelly ian.g.kelly at gmail.com
Thu Nov 13 21:33:40 CET 2014


On Thu, Nov 13, 2014 at 11:32 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 11/12/2014 01:51 PM, Ian Kelly wrote:
>>
>> On Wed, Nov 12, 2014 at 2:33 PM, Chris Kaynor wrote:
>>>
>>> A decorator is an interesting idea, and should be easy to implement (only
>>> lightly tested):
>>>
>>> def main(func):
>>>      if func.__module__ == "__main__":
>>>              func()
>>>      return func # The return could be omitted to block the function from
>>> being manually called after import.
>>
>> This calls it at the wrong time, though.  [...]
>
> One decorator that won't call too early is atexit.register().

Nice. That feels like an abuse, though. The actual program won't be
able to register its own atexit handlers, because the program will
already be exiting, and other things decorated with atexit.register
might actually be called before the main function.



More information about the Python-list mailing list