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

Chris Angelico rosuav at gmail.com
Mon Nov 25 16:56:22 CET 2013


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.

ChrisA


More information about the Python-ideas mailing list