descriptors for container items

bruno modulix onurb at xiludom.gro
Fri Sep 2 05:55:11 EDT 2005


Terry Reedy wrote:
> "Brock Filer" <filerba at softhome.net> wrote in message 
> news:ffdbcc8dcfa9ff84b34b3285e4d213a5 at softhome.net...
> 
>>countries['us']['Colorado']['Denver']['@population']
>>
>>This is going to be used in user-input formulae, so I'm willing to do a
>>lot of work for minor beautifications. I'd like to be able to say (I
>>know, the quotes are still ugly, but at least you save a bracket):
>>
>>countries/'us'/'Colorado'/'Denver'/'@population'
>>
>>That's easy to do with a __div__ method, but it only works for getting,
>>not setting or deleting.
>>
>>I'd appreciate any thoughts on this problem.
> 
> 
> I personally would first try to dump the quotes and use standard 
> attributes --  countries.us.Colorado... -- 

<aol/>

> and the  __get/set/delattr__  methods.
> 
>>I keep thinking descriptors might be involved somehow in the solution,
>>but I may be on a completely wrong track.
> 
> 
> As far as I know, 'descriptor' is a behind-the-scenes concept, not 
> something you directly program with.  Perhaps you meant 'property'. 

Properties are just syntactic sugar for a possible use of descriptors. A
descriptor is just an object that follows a specific protocol (__get__,
__set__, __del__) and is used as an attribute or method (the difference
is very thin) of another object. You'll find all the relevant doc on
python.org.

> However, properties are fixed in number when you create the class.

Dynamically adding attributes to an object (or a class - which is just
another object) is not a problem.

<op>
If what you need is to provide an higher level view of your data,
descriptors may be a good choice. I used them that way for a
higher-level ldap API, where the class representing the LDAP object
mainly stores the raw result (as returned by python-ldap) of the ldap
query and uses descriptors to provide controlled (an simpler) access to
the data.

You may also want to read a recent thread here about descriptors:
http://groups.google.fr/group/comp.lang.python/browse_frm/thread/6159fa26439b9ba5/0ad621b88a752b47
</op>

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list