2001 Enchancement Wishlist

Alex Martelli aleaxit at yahoo.com
Sat Jan 6 13:51:35 EST 2001


"Andrew Dalke" <dalke at acm.org> wrote in message
news:937l3u$gbi$1 at slb2.atl.mindspring.net...
    [snip]
>   proxy = Proxy(...)
>   sys.modules[module_name] = proxy
>
> In other words, what you import doesn't need to be a module.
> It can be a class instance instead - or any other data type.

Now THAT is interesting!  I would never have guessed that the
items in sys.modules need not be module objects.  Is this
documented, something I can rely on forevermore and just
happened not to notice in my study, or just an accident of the
current implementation which might go away anytime?

> This makes it possible to emulate the module __getattr__
> behaviour without changing the definition of a module.

Definitely.  If a module can overrite its own entry in sys.modules
with any other object-type, and this is 'solid' (documented, and
going to stay), then there is no particular need for any changes
to module-objects; it's not a _frequent_ occurrence for a module
to want to "take deep control of its own fate" (__setattr__ and
friends), so, when the requirement does arise, satisfying it with
the "overwrite sys.modules entry" hack seems OK to me.  Neat...!


Alex






More information about the Python-list mailing list