Having created a composite class in python, is there a (simple) way how to directly access the attributes/methods of the components? That is without specifying which component the attribute/method belongs to?
Here is the MWE:
class Component1:
def __init__(self, data1):
self.data1 = data1
def add1(self):
return self.data1 + 1
class Component2:
def __init__(self, data2):
self.data2 = data2
def add2(self):
return self.data2 + 2
class Composite:
def __init__(self, data1, data2):
self.component1 = Component1(data1)
self.component2 = Component2(data2)
composite = Composite(data1, data2)
# Now I can access data1 (and data2)
# But it is really cumbersome
composite.component1.data1
# So I would like to have just
composite.data1
Is there a way to achieve this especially when the number of attributes and methods is high? I know I could add the property
@property
def data1(self):
return self.component1.data1
but that would be painful with a high number of attributes and methods.
Edit:
Each component has different attributes/method names from each other. There also is not a common basis (eg, Component1().comp1_attibute_i
or anything similar). The number at the end of the names is not present in the real problem (it was used just for simplification).