I have an object that describes a dataset with multiple required pieces of data and metadata. The different data and metadata elements are themselves objects of the same type with a number of different attributes, including a name and a value attribute. Most of the interactions with these data elements are with the value attribute and it has become annoying to constantly reference it using .value, so I am trying to define a property for each data element, but I want to do it programmatically so when I add a new data element it automatically gets a property defined for it. To be specific, here is some example code:
class my_par(object):
def __init__(self, name, value):
self.name = name
self.value = value
class my_obj(object):
def __init__(self, xval, yval, zval):
self.xval = my_par('x', xval)
self.yval = my_par('y', yval)
self.zval = my_par('z', zval)
I can make properties on the my_obj class using
@property
def x(self):
return self.xval.value
@x.setter
def x(self, value):
self.xval.value = value
but I want a way to do it programmatically over all the my_par type objects. (I already have an iterator over those objects, so all I need is the code to define properties programmatically).