Search code examples
pythoninheritancemultiple-inheritance

calling multiple inheritance function is failing in python


I am trying to call an multiple inheritance function in python but i am getting some error

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()

error:

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'

Why does this error occurs and i need an ouput as prinited as 10


Solution

  • You are missing the constructor for Merge:

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