调用多继承函数在python中失败

时间:2018-05-04 07:51:55

标签: python inheritance multiple-inheritance

我试图在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

1 个答案:

答案 0 :(得分:0)

您缺少Merge的构造函数:

class Merge(A, B):
    def __init__(self):
       A.__init__(self)
       B.__init__(self)
    def disp(self):
        print self.num