[Python-ideas] descriptors outside of classes

Nick Coghlan ncoghlan at gmail.com
Thu Mar 31 01:00:37 CEST 2011

On Thu, Mar 31, 2011 at 8:04 AM, Michael Foord <fuzzyman at gmail.com> wrote:
>>> I was just thinking along those same lines.  Sounds like twisted already
>>> does it.  Does it amount to using a custom __import__?
>> I don't know what Twisted does,
> I'm pretty sure it creates a module subclass that forwards all attribute
> access to the real module and inserts itself into sys.modules in place of
> the "real" module. Pretty evil really. :-)
> I may be mistaken about this, it is based off my memory of a previous
> discussion.

That's certainly the trick people use to implement lazy import
handlers in the absence of a proper implementation of post-import
hooks (ala PEP 369). (I'll point out that the object inserted into
sys.modules doesn't need to be, and often isn't, an instance of the
module type)

The module code itself doesn't even need to know that the intervening
class exists.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list