[Python-ideas] Just __main__

Andrew McNabb amcnabb at mcnabbs.org
Mon Jun 18 22:09:33 CEST 2012


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.

> This will increase the python learning curve, because people will need to
> learn both the old method and the new method, especially since code that is
> compatible with multiple python versions will need to continue to use the
> old method. It saves one or two lines:
> 
>     if __name__ == '__main__': main()

If the only difference is saving a few lines, I agree that it probably
isn't worth it.  However, it also allows for a richer interactive mode
as I mentioned previously, so the benefit may not be limited to the
neglible number of lines saved.

--
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868



More information about the Python-ideas mailing list