471,350 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Module level descriptors or properties

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

Aug 21 '07 #1
2 2979
Floris Bruynooghe wrote:
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
Aug 21 '07 #2
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
Aug 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Ĺstrand | last post: by
3 posts views Thread by redefined.horizons | last post: by
13 posts views Thread by André | last post: by
7 posts views Thread by mrkafk | last post: by
reply views Thread by Michel Lespinasse | last post: by
reply views Thread by Tim Golden | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.