Object default value

ago agostino.russo at gmail.com
Tue Sep 20 23:01:23 CEST 2005


I am trying to write a generic DataAttribute class in order to simplify
access to object attributes and attached attribute-metadata for end
users with little programming experience.

Requirement 1: accessing the "default" value should be easy (using
assignment operator, via descriptors like __get__ and __set__).

Requirement 2: access to the attribute-metadata should also be as
intuitive as possible.

Requirement 3: possibly the DataAttribute should also be callable, to
return for instance, an history of its values.

class DataAttribute(object):
 value=1 #default
 old=2
 ....

class Obj(object): attr=DataAttribute()

#From end user prospective, ideally:
obj=Obj()
x = obj.attr #x=1
xold = obj.attr.old #xold=2
obj.attr = 3 #obj.attr.value=3
xold = obj.attr.old #xold=1
xhistory = obj.attr(startdate, enddate) #xhistory = [[date1,
4],[date2,5],[date3,6]]
xtimestmap = x.attr.timestamp
print obj.attr == obj.attr.value #True

If I use __get__ then I cannot access the metadata.

I could create a separate Obj attribute for each metadata item, but
then I would litter the Obj namespace (typical object will have several
attributes each with lots of metadata) and potentially create name
conflicts. Not to mention that DataAttributes should be
attached/deleted on the fly and if each attribute becames a set of
attributes this operation is complex (metclasses?).

If I use __call__ + __set__ but then I am introducing an asymmetry:

x = obj.attr()
obj.attr = 2

I could use getters and setters but that is not really convenient nor
intuitive




More information about the Python-list mailing list