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

Ethan Furman ethan at stoneleaf.us
Sun Dec 1 22:12:10 CET 2013


On 11/30/2013 06:03 PM, Gregory P. Smith wrote:
> On Wed, Nov 27, 2013 at 9:58 AM, Gregory Salvan 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.

+1 to gps' statements.

--
~Ethan~


More information about the Python-ideas mailing list