I have a list of Python objects, representing several classes. The classes obviously differ, but nevertheless have a couple of common attributes (with different values for each object). For example:
class Super1:
def __init__(self, value1, value2):
self.value1 = value1
self.value2 = value2
#Lot's of other stuff
class Sub1(Super1):
def __init__(self, value1, value2, value3):
Super1.__init__(self, value1, value2)
self.value3 = value3
#Lot's of stuff
class Sub2(Super1):
def __init__(self, value1, value2, value4):
Super1.__init__(self, value1, value2)
self.value4 = value4
#Lot's of stuff
objects = [Sub1(1.,2.,3.), Sub1(213.,2.,23.), Sub2(23.,10.,0.2), Sub1(3.,2.,12.)]
Now, for both convenience and performance, I would need to have to a NumPy array of all these values. I know I can read them like this:
np.array([objects[ii].value1 for ii in range(4)])
But I also need to change the values, in both the array form as well as individually within instance methods. Is it possible to somehow dynamically link the object attributes and the corresponding values in the arrays, by using pointers or something?
And no, the objects here does not have to be a list. Suggestions welcome.
np.array([o.value1 for o in objects])?