Module level descriptors or properties

F

Floris Bruynooghe

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
 
D

Diez B. Roggisch

Floris said:
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?

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

Diez
 
S

Steven Bethard

Floris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top