[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)
Chris Angelico
rosuav at gmail.com
Sat Nov 23 23:04:59 CET 2013
On Sun, Nov 24, 2013 at 5:00 AM, Bruce Leban <bruce at leapyear.org> wrote:
> If I had a time machine I would either do the __main__ function or
> alternatively:
>
> if __main__:
> ....
>
> but alas I can't time travel and changing it will *not* be simpler to learn
> because people will be seeing both the old idiom and the new idiom for a
> long time. While it's interesting to discuss from the standpoint of what's
> the best design, it's just not worth changing, IMHO.
+1. I quite like the simplicity of that, and it wouldn't even be
backward incompatible, but it wouldn't be backported.
Just a really crazy idea... Does Python let you go "one level outside"
and tinker with the code that imports __main__? I haven't looked into
all that mechanism, but I know quite a bit of it is now implemented in
Python, so it's theoretically possible... could you, in effect, add a
line of code *after* that import that effectively calls
__main__.__main__(sys.argv) ? That would do most of what you want.
ChrisA
More information about the Python-ideas
mailing list