Suppose I have two classes, parent and child as shown below:
class Child:
def __init__(self, name):
self.name = name
def change_name(self, name):
self.name = name
class Parent:
def __init__(self, child):
self.child = child
self.childs = [child]
def new_child (self, child):
self.childs.append[child]
self.child = child
Now if I create child object and parent object, then I want to call child properties from the parent as shown in the example below
child = Child('Nelson')
parent = Parent(child)
# I want to access child name from the parent object
print(parent.name) # should return parant.child.name <'Nelson'>
new_child = Child('Thomas')
parent.new_child(new_child)
print(parent.name) # should return the new name <'Thomas'>
# some code that will change the name of the child object
print(parent.name) # should return the new name
currently, I added a property decorator in the Parent class which returns the child property
class Parent:
def __init__(self, child):
self.child = child
self.childs = [child]
def new_child (self, child):
self.childs.append[child]
self.child = child
@property
def name(self):
return self.child.name
However, my child object has more than one property and I am looking for a more efficient way to inherit child properties into parent object