[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)

Chris Angelico rosuav at gmail.com
Sun Nov 24 17:55:38 CET 2013


On Mon, Nov 25, 2013 at 3:46 AM, Philipp A. <flying-sheep at web.de> wrote:
> 2013/11/24 Chris Angelico <rosuav at gmail.com>
>>
>> The decorator minorly worries me; what happens if you use it on two
>> functions? Presumably both would have to be called, in the order
>> they're in the file (or rather, the order the decorators are called),
>> but it'd be extremely confusing to try to read that code.
>>
>> ChrisA
>
> people can also do more than one if __name__ == '__main__' blocks ATM…

True, but the concept of a "main function" is going to look far more
like there should be only one. And it'd be extremely weird if:

@mainfunction
def main():
    print("First function!")
@mainfunction
def main():
    print("Second function!")

called both, even though the second one shadows the first; and it'd be
just as weird if:

@mainfunction
def main():
    print("First function!")
def main():
    print("Second function!")

called the second, even though it wasn't decorated as a main function.

ChrisA


More information about the Python-ideas mailing list