473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3105
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
14
by: Antoon Pardon | last post by:
Can anyone explain why descriptors only work when they are an attribute to an object or class. I think a lot of interesting things one can do with descriptors would be just as interesting if the...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
7
by: mrkafk | last post by:
Hello everyone, I'm trying to do seemingly trivial thing with descriptors: have another attribute updated on dot access in object defined using descriptors. For example, let's take a simple...
1
by: David | last post by:
I've always been in a hurry to write programs and I always end up writing alot of variables at the module level. Stuff that only needs to be in a procedure I will only define in a procedure but if...
0
by: Michel Lespinasse | last post by:
Hi, I hit an issue with the following python code: try: get_orient = subprocess.Popen (, stdin = subprocess.PIPE, stdout = subprocess.PIPE) orient = get_orient.communicate () except:
0
by: Tim Golden | last post by:
Michel Lespinasse wrote: This looks like a duplicate of http://bugs.python.org/issue3210. Can you confirm if this seems likely (and, if so, perhaps add a note to the bug indicating that the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.