[Python-ideas] lazy import via __future__ or compiler analysis

Neil Schemenauer nas-python-ideas at arctrix.com
Mon Sep 11 16:03:45 EDT 2017


On 2017-09-11, C Anthony Risinger wrote:
> I'm getting at, is can we find a way to make modules a real type? So dunder
> methods are activated? This would make modules phenomenally powerful
> instead of just a namespace (or resorting to after the fact __class__
> reassignment hacks).

My __namespace__ idea will allow this.  A module can be a singleton
instance of a singleton ModuleType instance.  So, you can assign a
property like:

    <this module>.__class__.prop = <property>

and have it just work.  Each module would have a singleton class
associated with it to store the properties.  The spelling of
<this module> will need to be worked out.  It could be
sys.modules[__name__].__class__ or perhaps we can have a weakref, so
this:

    __module__.__class__.prop = ...

Need to think about this.

I have done import hooks before and I know the pain involved.
importlib cleans things up a lot.  However, if my early prototype
work is an indication, the import stuff gets a whole lot simpler.
Instead of passing around a dict and then grubbing around
sys.modules because the module is actually what you want, you just
pass the module around directly.

Thanks for you feedback.

Regards,

  Neil


More information about the Python-ideas mailing list