Module level descriptors or properties

Steven Bethard steven.bethard at gmail.com
Tue Aug 21 13:31:44 EDT 2007


Floris Bruynooghe wrote:
> When in a new-style class you can easily transform attributes into
> descriptors using the property() builtin.  However there seems to be
> no way to achieve something similar on the module level, i.e. if
> there's a "version" attribute on the module, the only way to change
> that to some computation later is by using a getter from the start as
> your public API.  This seems ugly to me.
> 
> Does anyone know of a better way to handle this?

Better is of course subjective, but you can always do something like::


     class ModuleWrapper(...):
         def __init__(self, module):
             ...
         ...
         x = property(...)

     sys.modules[__name__] = ModuleWrapper(sys.modules[__name__])

STeVe



More information about the Python-list mailing list