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

Antoine Pitrou solipsis at pitrou.net
Sun Nov 24 22:26:07 CET 2013


On Sun, 24 Nov 2013 11:10:43 -0800
Guido van Rossum <guido at python.org> wrote:
> Haven't followed all of this, but perhaps the simplest thing would be to
> define a new builtin function that returns True in the namespace of the
> main module and false everywhere else. It could be implemented by pulling
> '__name__' out of the caller's local namespace and comparing it to
> '__main__'. We could name this function __main__(), or perhaps less
> dramatic, is_main(). Then you could write
> 
> if is_main():
>     <do your main code>

Why not make it so that a module function named __main__, if it exists,
gets executed when the module is run as a script?

(this would also mimick the __main__.py convention for packages)

Regards

Antoine.




More information about the Python-ideas mailing list