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

Eric Snow ericsnowcurrently at gmail.com
Fri Nov 22 22:40:40 CET 2013


On Fri, Nov 22, 2013 at 1:44 PM, Gregory P. Smith <greg at krypto.org> wrote:
> It'd be nice to formalize a way to get rid of the __name__ == '__main__'
> idiom as well in the long long run. Sure everyone's editor types that for
> them now but it's still a wart.  Anyways, digressing... ;)

This has come up before and is the subject of several PEPS. [1][2]
The current idiom doesn't bother me too much as I try not to have
files that are both scripts and modules.  However, Python doesn't make
the distinction all that clear nor does it do much to encourage people
to keep the two separate.  I'd prefer improvements in both those
instead, but haven't had the time for any concrete proposal.

FWIW, aside from the idiom there are other complications that arise
from a module that also gets loaded in __main__ (run as a script).
See PEP 395 [3].

-erc


[1] http://www.python.org/dev/peps/pep-0299/
[2] http://www.python.org/dev/peps/pep-3122/
[3] http://www.python.org/dev/peps/pep-0395/ (sort of related)


More information about the Python-ideas mailing list