[Python-ideas] Modules could behave like new-style objects

Thomas Heller theller at ctypes.org
Fri Apr 3 14:57:26 CEST 2009


Christian Heimes schrieb:
> Zac Burns schrieb:
>> I would like to see modules behave more like new-style objects. One
>> should be able to add properties, descriptors, __getattr__, and such.
>> 
>> One common use case for me is implementing wrapper modules that
>> populate dynamically (like the ctypes.windll object - except as a
>> module).
> 
> Modules are new style classes and they behave exactly like new style
> classes. Perhaps you have missed the point that modules are *instances*
> of the module type? As you probably know one can't overwrite magic
> methods like __getattr__ on the instance. :)

But one could probably implement the module type's __getattr__ method
to delegate to the instance, somehow.  Yes, I know that Guido doesn't like it.

What one could also do is to replace the module object in sys.modules
with a magic object that specializes the behaviour.
The 'include' function in this module does it:

http://svn.python.org/view/ctypes/trunk/ctypeslib/ctypeslib/dynamic_module.py?revision=HEAD&view=markup

-- 
Thanks,
Thomas




More information about the Python-ideas mailing list