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

Haoyi Li haoyi.sg at gmail.com
Sun Nov 24 04:07:43 CET 2013


Import hooks don't work with __main__ =( I spent a while trying to get it
to work when working on MacroPy, to no avail.


On Sat, Nov 23, 2013 at 4:37 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> On Sun, Nov 24, 2013 at 09:04:59AM +1100, Chris Angelico wrote:
>
> > 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.
>
> It sounds like you're describing an import hook, although such things
> are completely opaque to me. I know they exist, but I've got no idea how
> they work or what they can do.
>
>
> --
> Steven
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131123/d24383ad/attachment.html>


More information about the Python-ideas mailing list