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

Guido van Rossum guido at python.org
Mon Nov 25 20:50:15 CET 2013


On Mon, Nov 25, 2013 at 11:42 AM, Barry Warsaw <barry at python.org> wrote:

> On Nov 26, 2013, at 01:12 AM, Steven D'Aprano wrote:
>
> >(1) Keep the status quo.
>
> I'd have no problem with this.  The current idiom doesn't seem broken to
> me,
> nor is it that hard to type.  I also don't think it's very hard to discover
> given how common it is.
>

All agreed, yet it is not that easy to type either (underscores and quotes
require the shift key). Perhaps more important, it causes everyone who sees
it first to wonder why the idiom isn't simpler.


> >if __main__:
>
> If we *had* to make this easier to type, this would be my choice.  It
> doesn't
> even have to be read-only, given that __name__ can be messed with, but
> usually
> isn't.  Why then worry about __main__ getting messed with?
>

The problem with this is, how would you implement this? You can either make
__main__ a builtin object with a magic __bool__() method, or you can plunk
a bool named __main__ in every module namespace. I don't much like either.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131125/62ef1a9f/attachment-0001.html>


More information about the Python-ideas mailing list