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

Vito De Tullio vito.detullio at
Sun Nov 16 11:26:50 CET 2014

Steven D'Aprano wrote:

> Chris Kaynor wrote:
>> I was thinking along the lines of replacing:
>> if __name__ == "__main__":
>> <<<block of code>>>
>> with
>> @main
>> def myFunction()
>> <<<<block of code>>
>> Both blocks of code will be called at the same time.
> You can't guarantee that, because you cannot tell ahead of time when the
> "if __name__" statement will be run. It is *usually* at the end of the
> file, but that's just the normal useful convention, it is not a hard
> requirement.

sure you can: simply the main decorator is just something like

def main(myMainFunction):
    if myMainFunction.__module__ == '__main__':
    return myMainFunction

By ZeD

More information about the Python-list mailing list