[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)
Ethan Furman
ethan at stoneleaf.us
Mon Nov 25 23:17:53 CET 2013
On 11/25/2013 02:12 PM, Ron Adam wrote:
> On 11/25/2013 09:56 AM, Chris Angelico wrote:
>> On Tue, Nov 26, 2013 at 2:35 AM, Ron Adam 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.
Yes, it is bad practice to use `is` this way. The only time `is` should be used is when you need to know that two
names/references are referring to the exact same object, which is definitely not the case here.
--
~Ethan~
More information about the Python-ideas
mailing list