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

אלעזר elazarg at gmail.com
Mon Nov 25 23:25:56 CET 2013


2013/11/24 Guido van Rossum <guido at python.org>
>
> if is_main():
>     <do your main code>
>

How about going the other way around?

    if imported():
        break

    <do your main code>

Most of the time, people put the main stuff at the end of the script, so
this check can serve as a seperator, equivalent to what is sometimes marked
with '***********************' - snip the script here.

Of course, it is still possible to do

    if not imported():
        main()

I think it is at least as obvious as is_main(), and even more so for those
without a C-like background.

(Personally I'd prefer an `imported` magic variable but I guess that's out
of question).

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131126/cba86bff/attachment.html>


More information about the Python-ideas mailing list