python 中属性的子属性
sub-attribute of attribute in python
与this类似的问题,我想知道在python中实现属性的right/common方法是什么。或者我这样的想法已经错了。以下是示例:
方法一
很明显不允许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'
方法二
使用不带属性属性的字典效果很好,但我倾向于使用方法 1,尽管由于某些原因它不起作用。
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)
方法 1 不起作用的原因正是错误消息给您的原因:您无法为不存在的属性创建 sub-attribute。您必须将 self.father
初始化为具有兼容结构的东西; 然后 您可以分配给 child 属性。
分配字典是实现预期结果的好方法。
如果我要实现这个,我会将 name/height 对作为条目的列表或元组以供以后扩展。有些人有一个parent;其他人有两个以上。
household("198 Ave", [("John", 6.4), ("Jean",5.2)] )
看来你可以使用 namedtuple:
得到接近你想要的东西
from collections import namedtuple
class household:
def __init__(self, address, name0, height0, name1, height1 ):
self.address = address
Parent = namedtuple('Parent', ['name', 'height'])
self.father = Parent(name0, height0)
self.mother = Parent(name1, height1)
示例:
h = household("198 Ave", "John", 6.4, "Jean",5.2)
print(h.father.name, h.father.height)
它给出:
John 6.4
你需要做的是首先需要初始化self.father然后使用self.father.name.
您还应该从 python 文档
中阅读有关 OOP 的更多信息
不知道typical/common的实现方式是哪种。但当然,我们可以让方法1发挥作用。 @angadsinghbedi 是对的,我们可以使用 type("",(),{})
将 self.father
初始化为有效对象。有关创建空对象的更多详细信息,请参阅 here
class household:
def __init__(self, address, name0, height0, name1,height1 ):
self.address = address
self.father = type("person", (),{})()
self.father.name = name0
self.father.height = height0
self.mother = type("person", (), {})()
self.mother.name = name1
self.mother.height = height1
house0 = household("198 Ave", "John", 6.4, "Jean",5.2)
house0.father.name
# John
与this类似的问题,我想知道在python中实现属性的right/common方法是什么。或者我这样的想法已经错了。以下是示例:
方法一
很明显不允许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'
方法二
使用不带属性属性的字典效果很好,但我倾向于使用方法 1,尽管由于某些原因它不起作用。
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)
方法 1 不起作用的原因正是错误消息给您的原因:您无法为不存在的属性创建 sub-attribute。您必须将 self.father
初始化为具有兼容结构的东西; 然后 您可以分配给 child 属性。
分配字典是实现预期结果的好方法。
如果我要实现这个,我会将 name/height 对作为条目的列表或元组以供以后扩展。有些人有一个parent;其他人有两个以上。
household("198 Ave", [("John", 6.4), ("Jean",5.2)] )
看来你可以使用 namedtuple:
得到接近你想要的东西from collections import namedtuple
class household:
def __init__(self, address, name0, height0, name1, height1 ):
self.address = address
Parent = namedtuple('Parent', ['name', 'height'])
self.father = Parent(name0, height0)
self.mother = Parent(name1, height1)
示例:
h = household("198 Ave", "John", 6.4, "Jean",5.2)
print(h.father.name, h.father.height)
它给出:
John 6.4
你需要做的是首先需要初始化self.father然后使用self.father.name.
您还应该从 python 文档
中阅读有关 OOP 的更多信息不知道typical/common的实现方式是哪种。但当然,我们可以让方法1发挥作用。 @angadsinghbedi 是对的,我们可以使用 type("",(),{})
将 self.father
初始化为有效对象。有关创建空对象的更多详细信息,请参阅 here
class household:
def __init__(self, address, name0, height0, name1,height1 ):
self.address = address
self.father = type("person", (),{})()
self.father.name = name0
self.father.height = height0
self.mother = type("person", (), {})()
self.mother.name = name1
self.mother.height = height1
house0 = household("198 Ave", "John", 6.4, "Jean",5.2)
house0.father.name
# John