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

David Mertz mertz at gnosis.cx
Fri Nov 22 23:02:27 CET 2013


I'm not in love with the *spelling* of " if __name__=='__main__': ", but I
very frequently use the overall pattern.

Much--or even most--of the time when I write a module, I like to allow it
to either do a minimal case of its basic functionality and/or have the
module run some basic unit tests as a quick check against breakage.  So in
contrast to Eric Snow, I try *to* make my files both scripts and modules.
 I know this isn't the only possible approach, but I don't think it's bad
or uncommon.


On Fri, Nov 22, 2013 at 1:40 PM, Eric Snow <ericsnowcurrently at gmail.com>wrote:

> 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)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131122/54b4cc04/attachment.html>


More information about the Python-ideas mailing list