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

Ron Adam ron3200 at gmail.com
Tue Nov 26 04:29:33 CET 2013



On 11/25/2013 09:04 PM, Alexander Belopolsky wrote:
> That's news to me.  It was not so as of 3.3.3:
>
> Python 3.3.3 (default, Nov 25 2013, 15:41:46)
>  >>> def f():
> ...     print(__name__)
> ...
>  >>> f()
> __main__
>
> If anything, it is a magic function relying on _getframe() run-time
> introspection that would have a problem working inside functions.

Woops..  yep.

Hmmm,  Wonder what I came across recently to make me think that.  Oh well.

I still think there would be some advantages from comparing the actual 
modules rather than the __name__ attribute.

Jan pointed out some use cases.

Cheers,
    Ron



More information about the Python-ideas mailing list