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

Gregory P. Smith greg at krypto.org
Sun Dec 1 03:03:21 CET 2013


On Wed, Nov 27, 2013 at 9:58 AM, Gregory Salvan <apieum at gmail.com> wrote:

> my cent:
> I think it's most often a bad practice (whereas it's convenient) to mix
> "executable" and "importable" code.
> Providing an easier way to do it, may encourage a bad practice.
>
> I would prefer a solution which encourage separation of "executable" and
> "importable" code.
>

The only way to do that is to force people to put their executable code in
a __main__.py file and prevent class and function definitions within
that...  Gross.

People need the test for main or not (imported or not) because they write
unit tests for _all_ of their code, including their main program.  Just
because it can be imported does not mean it is a library that anyone
outside of their tests _should_ import. Sure, there are some python stdlib
modules and others that work as an importable library or via python -m
modulename to run a program but I wouldn't call that the common case.  I
would not want that to stop working either.

-gps



>
>
>
> 2013/11/26 Andrew Barnert <abarnert at yahoo.com>
>
>> On Nov 26, 2013, at 8:34, Alan Cristhian Ruiz <alan.cristh at gmail.com>
>> wrote:
>>
>> > I think the need to change * if __ name__ == "__main__": * is
>> capricious and obsessive. The current rule is better than anything that has
>> been suggested so far. I never had any problems with the * if __ name__ ==
>> "__main__": *. Also in python there are other things much more difficult to
>> learn and use, such as metaclasses.
>>
>> Although I agree with your main point, I don't think that's a very good
>> argument.
>>
>> __main__ is something novices have to learn early and use in code
>> regularly; metaclasses are something only experienced developers use, and
>> not that often (and that's even if you count using stdlib metaclasses to,
>> e.g., create ABCs, which doesn't really require you to understand how they
>> work). It's perfectly reasonable for an "expert" feature to be more
>> difficult to learn than a novice feature.
>>
>> Also, Python doesn't have a queue of improvements to be scheduled to a
>> team of developers. Things get improved if someone is motivated enough to
>> write the code and drive the idea to consensus and/or BDFL approval. So,
>> improving this would have very little bearing on improving things you care
>> about more.
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131130/4eab0db5/attachment.html>


More information about the Python-ideas mailing list