[Python-Dev] Dynamic module namspaces

Johan Dahlin jdahlin at async.com.br
Mon Jul 17 15:29:22 CEST 2006


Phillip J. Eby wrote:

> Just as a point of reference, the Importing package does something very
> similar, to support "weak" and "lazy" imports:
> 
> http://cheeseshop.python.org/pypi/Importing

Interesting, I was not aware of that, thanks for the pointer.
Another reason for including this feature in the standard library ;-)

> The things most likely to be problems are tools like pydoc or other
> inspect-using code that expects modules to be exactly ModuleType and
> don't use isinstance().  Apart from that, I've been using the technique
> since the early days of Python 2.2 without encountering any problems
> until the PEP 302 "reload()" bug came along, but that was fixed in
> 2.3.5.  I haven't seen any other problems since.

I'd argue that pydoc & friends are broken if they assume that a module will
always be of ModuleType and not a subclass of it.

> On the other hand, the Importing package takes a much more conservative
> approach than you are doing; it simply runs reload() on a module when
> __getattribute__ is called (after restoring the old version of
> __getattribute__).  So, as soon as you touch a lazily loaded module, it
> ceases to be particularly special, and it has a real __dict__.  It's
> possible that what you're doing could have more side-effects than what
> I'm doing.

This is an interesting approach, I thought of using that but I didn't quite
manage to find the time to implement it properly.

However, for the gtk namespace this won't be enough, since I want to avoid
creating all the types when the first one is accessed; when gtk.Button is
accessed it, gtk.Window will still not be created.

>> What I want to ask, is it possible to have a sanctioned way to implement
>> a dynamic module/namespace in python?
> 
> That would be nice, but I think that what you and I are doing are
> probably the One Obvious Ways to do the respective things we're doing.

I consider __getattribute__ a hack, being able to override __dict__ is less
hackish, IMHO.

-- 
Johan Dahlin <jdahlin at async.com.br>
Async Open Source


More information about the Python-Dev mailing list