[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)
MRAB
python at mrabarnett.plus.com
Tue Nov 26 01:59:40 CET 2013
On 25/11/2013 22:25, אלעזר wrote:
>
>
>
> 2013/11/24 Guido van Rossum <guido at python.org <mailto: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).
>
Instead of "imported", how about "import"? That already exists as a
reserved word and its use outside an import statement is currently
illegal:
if not import:
main()
More information about the Python-ideas
mailing list