Module level descriptors or properties

0 views
Skip to first unread message

Floris Bruynooghe

unread,
Aug 21, 2007, 8:48:59 AM8/21/07
to
Hi

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?


Regards
Floris

Diez B. Roggisch

unread,
Aug 21, 2007, 9:46:33 AM8/21/07
to
Floris Bruynooghe wrote:

Not really. All you can do is proxy all the calls through an actual object,
most probably a singleton.

Diez

Steven Bethard

unread,
Aug 21, 2007, 1:31:44 PM8/21/07
to

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

Reply all
Reply to author
Forward
0 new messages