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

Ron Adam ron3200 at gmail.com
Tue Nov 26 03:37:49 CET 2013


On 11/25/2013 04:50 PM, Guido van Rossum wrote:
> On Mon, Nov 25, 2013 at 2:39 PM, Barry Warsaw
> <barry at python.org
> <mailto:barry at python.org>> wrote:
>
>     On Nov 25, 2013, at 02:29 PM, Guido van Rossum wrote:
>
>      >For all I care you can call it ismain().
>
>     Okay, I think I'm going to officially not care now.  :)   None of these
>     suggestions seem worth the effort to indoctrinate folks to some new idiom,
>     regardless of how it's spelled.
>
>
> I mostly agree -- but if people insist on a better idiom, a builtin
> function is the only one I can live with. I don't particularly care what
> that builtin function is called, as long as it is not a __dunder__ name.
> And it must be a function.


I'm +1 on the builtin function alternative if it compares the actual
modules rather than just the names.  Then it would have some benefits I think.

It could be used within functions. Currently __name__ is masked by the
function's __name__ attribute if you try that.

It would still work if someone overwrites __name__. Not that I think it 
happens very often.

And I don't care if it's spelled ismain or is_main.  Well actually I like 
the underscores separating words in function names.  But we have 
isattribute() which already doesn't follow that.  So which ever is more 
consistent.

Cheers,
    Ron



More information about the Python-ideas mailing list