[Python-ideas] Just __main__

Massimo DiPierro massimo.dipierro at gmail.com
Mon Jun 18 21:58:38 CEST 2012

how about a decorator that have the same effect as calling the function is __name__=='__main__'?

On Jun 18, 2012, at 2:39 PM, Bruce Leban wrote:

> On Mon, Jun 18, 2012 at 11:05 AM, Andrew McNabb <amcnabb at mcnabbs.org> wrote:
> I agree that having a boolean called "__main__" wouldn't add much value,
> but I believe that recognizing a function called "__main__" could
> potentially add a bit more value.
> After executing the body of a script, the interpreter would
> automatically call the "__main__" function if it exists, and exit with
> its return value.
> 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.
> 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()
> A __main__ boolean, that saves even less typing, and does not seem worth adding either.
> -1 for both
> --- Bruce
> Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120618/3230834d/attachment.html>

More information about the Python-ideas mailing list