Similar question to this, I was wondering what is the right/common way to implement attribute of an attribute in python. Or I am wrong already with such a thought. Here are the examples:
method 1
It's obvious that it doesn't allow self.father.height
class household:
def __init__(self, address, name0, height0, name1,height1 ):
self.address = address
self.father.name = name0
self.father.height = height0
self.mother.name = name1
self.mother.height = height1
household("198 Ave", "John", 6.4, "Jean",5.2)
output:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-180-09f6784cb40e> in <module>
----> 1 household("198 Ave", "John", 6.4, "Jean",5.2)
<ipython-input-179-9ccb52cdb476> in __init__(self, address, name0, height0, name1, height1)
2 def __init__(self, address, name0, height0, name1,height1 ):
3 self.address = address
----> 4 self.father.name = name0
5 self.father.height = height0
6 self.mother.name = name1
AttributeError: 'household' object has no attribute 'father'
method 2
It works well by using a dictionary without the attribute of an attribute, but I tend to use method 1 although it does not work for some reason.
class household:
def __init__(self, address, name0, height0, name1,height1 ):
self.address = address
self.father = {"name":name0,"height":height0}
self.mother = {"name":name1,"height":height1}
household("198 Ave", "John", 6.4, "Jean",5.2)