Why 'AttributeError' when I am inheriting the base class already?
对不起,我是新手。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class A(object): def __init__(self, idn, name): self.idn = idn self.name = name class B(object): def __init__(self, idn, acc_no, criminal_case='No'): self.idn = idn self.acc_no = acc_no self.criminal_case = criminal_case def get_info(self): return self.idn class C(A, B): pass c = C(1, 'xyz') print c.get_info() print c.criminal_case |
回溯(最近一次呼叫的最后一次):
文件"tp.py",第25行,in
打印刑事案件
attributeError:"c"对象没有"criminalu case"属性
没有
超级()
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class.
您的代码应该如下所示:
1 2 3 4 5 | class A(object): def __init__(self, idn, name): super(A, self).__init__(idn, name,'test') self.idn = idn self.name = name |
你会得到输出:
1 2 | 1 test |
在python 3.x中,您只需使用
希望这有帮助。
python不为继承层次结构中的每个类调用
要从
1 | super(A, self).__init__(idn, None) |
但这并没有多大意义: