Python Multiple Inheritance

When a class inherits from more than one class, it’s called multiple inheritances. Python supports multiple inheritances whereas Java doesn’t support it. The properties of all the super/base classes are inherited into the derived/subclass.

Python Multiple Inheritance
Python Multiple Inheritance

Python Multiple Inheritance Syntax

The syntax for Multiple Inheritance is also similar to the single inheritance. By the way, the derived class claims the properties and methods of all the base classes.

class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass

Python Multiple Inheritance Example

# first parent class 
class Manager(object):                   
      def __init__(self, name, idnumber): 
            self.name = name 
            self.idnumber = idnumber 
  
# second parent class       
class Employee(object):                 
      def __init__(self, salary, post): 
            self.salary = salary 
            self.post = post 
  
# inheritance from both the parent classes       
class Person(Manager, Employee):         
      def __init__(self, name, idnumber, salary, post, points): 
            self.points = points 
            Manager.__init__(self, name, idnumber) 
            Employee.__init__(self, salary, post)    
            print(self.salary) 
        
ins = Person('Rahul', 882016, 75000, 'Assistant Manager', 560)

Output: 75000


Multiple resolution order

Method Resolution Order (MRO) is an approach that takes to resolve the variables or functions of a class.

  • In the multiple inheritance use case, the attribute is first looked up in the current class. If it fails, then the next place to search is in the parent class.
  • If there are multiple parent classes, then the preference order is depth-first followed by a left-right path.
  • MRO ensures that a class always precedes its parents and for multiple parents, keeps the order as the tuple of base classes and avoids ambiguity.

Example to explain MRO

class Agile:
    def create(self):
        print(" Forming class Agile")


class Dev(Agile):
    def create(self):
        print(" Forming class Dev")


class QA(Agile):
    def create(self):
        print(" Forming class QA")


# Ordering of classes
class Sprint(Dev, QA):
    pass


sprint = Sprint()
sprint.create()

Output: Forming class Dev


Methods For Method Resolution Order(MRO)

Python provides a __mro__ attribute and the mro() method. With these, you can get the resolution order.

class Material:
    def create(self):
        print(" Creating class Appliance")


class Pencil:
    def create(self):
        print(" Creating class Pencil")

    # Order of classes


class Pen(Material, Pencil):
    def __init__(self):
        print("Constructing Pen")


appl = Pen()

# Display the lookup order
print(Pen.__mro__)
print(Pen.mro())

Output:

Constructing Pen
(<class '__main__.Pen'>, <class '__main__.Material'>, <class '__main__.Pencil'>, <class 'object'>)
[<class '__main__.Pen'>, <class '__main__.Material'>, <class '__main__.Pencil'>, <class 'object'>]

References: