[Python-ideas] Just __main__

Ethan Furman ethan at stoneleaf.us
Mon Jun 18 22:38:31 CEST 2012


Antoine Pitrou wrote:
> On Mon, 18 Jun 2012 14:09:33 -0600
> Andrew McNabb <amcnabb at mcnabbs.org> wrote:
>> On Mon, Jun 18, 2012 at 12:39:05PM -0700, Bruce Leban wrote:
>>> The special value of __name__ and the proposed __main__() function are both
>>> a bit magic. However, when I write if __name__ == '__main__' it's at least
>>> clear that that if statement *will* be executed. It's just a question of
>>> when the condition is true and if I don't know I can find out fairly
>>> easily. (As I did the first time I saw it and probably other people on this
>>> list did too.) On the other hand, it's not at all obvious that a function
>>> named __main__ will be executed automagically.
>> Given that C, Java, and numerous other languages automagically execute a
>> function called "main", I would argue that a "__main__" function would
>> actually be _less_ surprising than "if __name__ == '__main__'" for most
>> new Python users.
> 
> Yes, a __main__ function would be reasonable, especially now that we
> have __main__.py files in packages.
> 
> Massimo's suggestion of a decorator, OTOH, sounds useless: how would it
> help in any way?

I've actually tried the @main decorator approach, and found it not worth 
the trouble -- I went back to 'if __name__ == "__main__"'.

~Ethan~



More information about the Python-ideas mailing list