>我正在尝试在python中调用多重继承函数,但是我遇到了一些错误
class A:
def __init__(self):
self.name = "kiran"
class B:
def __init__(self):
self.num = 10
class Merge(A,B):
def disp(self):
print self.num
obj_var = Merge()
print obj_var.disp()
错误:
Traceback (most recent call last):
File "test_calss_obj.py", line 12, in <module>
print obj_var.disp()
File "test_calss_obj.py", line 9, in disp
print self.num
AttributeError: Merge instance has no attribute 'num'
为什么会发生此错误,我需要像 10 一样的输出
您缺少 Merge 的构造函数:
class Merge(A, B):
def __init__(self):
A.__init__(self)
B.__init__(self)
def disp(self):
print self.num