Multiple Inheritance


Multiple inheritance is possible in Python unlike other programming languages. A class can be derived from more than one base classes. The syntax for multiple inheritance is similar to single inheritance.


In [1]:
class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass

In [3]:
class Base1:
    def test(self):
        print("in Base1 -> test")

class Base2:
    def test(self):
        print("in Base2 -> test")

class MultiDerived(Base1, Base2):
    def test2(self):
        super().test()
        Base2.test(Base2)

class MultiDerived2(Base2, Base1):
    def test2(self):
        super().test()
        Base2.test(Base2)

print("Please check the result of test()")

# d = Base2()
# print(type(d))
md = MultiDerived()
md.test2()
md.test()
print("*"*10)
md2 = MultiDerived2()
md2.test2()
md2.test()


Please check the result of test()
in Base1 -> test
in Base2 -> test
in Base1 -> test
**********
in Base2 -> test
in Base2 -> test
in Base2 -> test

In [ ]:


In [ ]:

Multilevel Inheritance


we can inherit to from a derived class also. This is called as multilevel inheritance. Multilevel inheritance can be of any depth in Python. An example with corresponding visualization is given below.


In [ ]:
class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass

In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice


In [1]:
class Base:
    def test(self):
        print("In Base test")
    
    def test_alone(self):
        print("In Base test: test_alone")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        super().test()

    def test_alone(self, val):
        print("In Derived1 test: test_alone ", val)

#     def test_alone(self):
#         print("In Base test: test_alone")

class Derived2(Derived1):
    def test2(self):
        print("in Derived2 test2")
        
obj = Derived2()
obj.test()
obj.test2()
obj.test_alone()
Base.test(Base)


In Derived1 test
In Base test
in Derived2 test2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-ad5ea19a6861> in <module>()
     24 obj.test()
     25 obj.test2()
---> 26 obj.test_alone()
     27 Base.test(Base)

TypeError: test_alone() missing 1 required positional argument: 'val'

In [9]:
class Base:
    def test(self):
        print("In Base test")
    
    def test_base(self):
        print("test_base")

    def test_alone(self):
        print("In Base test: test_alone")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        super().test()
        self.test_base()
    
    def test_alone(self, val):
        print("In Derived1 test: test_alone ", val)
        self.test()

        
obj = Derived1()
obj.test()
obj.test_alone("test")
Base.test(Base)


In Derived1 test
In Base test
test_base
In Derived1 test: test_alone  test
In Derived1 test
In Base test
test_base
In Base test

In [11]:
class Base:
    def test(self):
        print("In Base test")
    
    def test_base(self):
        print("test_base")

    def test_alone(self):
        print("In Base test: test_alone")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        super().test()
        Base.test_base(Base)
    
    def test_alone(self, val):
        print("In Derived1 test: test_alone ", val)
        self.test()

        
obj = Derived1()
obj.test()
obj.test_alone("test")
Base.test(Base)


In Derived1 test
In Base test
test_base
In Derived1 test: test_alone  test
In Derived1 test
In Base test
test_base
In Base test

In [6]:
class Base:
    def test(self):
        print("In Base test")
        
    def test_alone(self):
        print("In Base test: test_alone")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        super().test()

    def test_alone(self, val):
        print("In Derived1 test: test_alone ", val)

    def test_alone(self):
        print("In Base test: test_alone")

class Derived2(Derived1):
    def test2(self):
        print("in Derived2 test2")
        
obj = Derived2()
obj.test()
obj.test2()
obj.test_alone()
Base.test(Base)


In Derived1 test
In Base test
in Derived2 test2
In Base test: test_alone
In Base test

In [3]:
class Base():
    def test1(self):
        print("In Base test")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")

class Derived3(Derived1):
    pass

d = Derived3()
d.test()
d.test1()


In Base test

In [27]:
class Base():
    def test(self):
        print("In Base test")

class Derived1(Base):
    def test(self):
        print("In Derived1 test", end=", ")
        return "Golu"

class Derived3(Derived1):
    pass

d = Derived3()
print(d.test())


In Derived1 test, Golu

In [39]:
#### Explicitly calling function

class Base:
    def test(self):
        print("In Base test")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")

class Derived2(Derived1):
    pass

obj = Derived2
obj.test(obj)

Derived2.test(Derived2)


In Derived1 test
In Derived1 test

In [41]:
#### Explicitly calling function

class Base:
    def test(self):
        print("In Base test")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        print(type(self))

class Derived2(Derived1):
    pass

obj = Derived2
obj.test(obj)

Derived2.test(Derived2)


In Derived1 test
<class 'type'>
In Derived1 test
<class 'type'>

In [61]:
#### Explicitly calling function

class Base:
    def test(self):
        print("In Base test")

class Derived1(Base):
    def test(self):
        print("In Derived1 test")
        print(type(self))

class Derived2(Derived1):
    pass


obj = Derived2()
obj.test()

Derived2.test(Derived2)


In Derived1 test
<class '__main__.Derived2'>
In Derived1 test
<class 'type'>

In [ ]: