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

Ron Adam ron3200 at gmail.com
Mon Nov 25 23:12:10 CET 2013


On 11/25/2013 09:56 AM, Chris Angelico wrote:
> On Tue, Nov 26, 2013 at 2:35 AM, Ron Adam<ron3200 at gmail.com>  wrote:
>> >But in this case...
>> >
>> >        __main__ = "__main__"
>> >        __name__ = __main__
>> >        assert __main__ is __name__
>> >
>> >That should always work.  I would think it was a bug if it didn't.
> Of course, that will work. But this isn't guaranteed to:
>
> __main__ = "__main__"
> __name__ = "__main__"
> assert __main__ is __name__
>
> It quite possibly will, but it's not guaranteed by the language. And
> this is more what's being done here - a separate literal.

Are thinking that the __main__ global would be defined after the main 
module is loaded?

I was thinking that __main__ would be set to "__main__" first, then when 
the main module is loaded, it's __name__ attribute set to __main__ rather 
than "__main__".  Exactly as the first example above.

Or are you thinking there may be more than one main module?

If you just feel strongly that using 'is' is a bad practice in this case, 
I'm fine with that.

Cheers,
    Ron




More information about the Python-ideas mailing list