[Python-Dev] Dynamic module namspaces
Johan Dahlin
jdahlin at async.com.br
Sat Jul 15 20:38:04 CEST 2006
In an effort to reduce the memory usage used by GTK+ applications
written in python I've recently added a feature that allows attributes
to be lazy loaded in a module namespace. The gtk python module contains
quite a few attributes (around 850) of which many are classes or
interfaces (150+)
The changes to PyGTK I had to make can not be considered anything but a
hack; I had to put a subclass of a ModuleType in sys.modules and
override __getattribute__ to be able to get old code which accessed
gtk.__dict__ directly to still work (PyModule_GetDict requires that).
However, even if I didn't have to use __getattribute__ overriding
sys.modules is rather unpleasent and I'm afraid it'll cause problems in
the future.
My point is that I consider this to be a valid use case, the amount of
saved memory is significan, and I could not find another way of doing it
and still keep the gtk interface (import gtk; gtk.Button) to still be
backwards compatible.
What I want to ask, is it possible to have a sanctioned way to implement
a dynamic module/namespace in python?
For instance, it could be implemented to allow you to replace the
__dict__ attribute in a module with a user provided object which
implements the dictionary protocol.
Johan
More information about the Python-Dev
mailing list