Zone Of Makos

Menu icon

Multiple Inheritance in Python

Inheritance is a powerful feature of object-oriented programming (OOP) that allows you to create a new class based on an existing class. Python supports multiple inheritance, which means a class can inherit from multiple parent classes. This allows you to create more complex class hierarchies and reuse code more efficiently. In this lesson, we'll explore how to use multiple inheritance in Python and some best practices to follow.

Creating a Class with Multiple Inheritance

To create a class with multiple inheritance in Python, you simply list the parent classes in parentheses after the name of the derived class, separated by commas. The derived class will inherit all the attributes (methods and properties) of each parent class, in the order they are listed. If a method or property is defined in more than one parent class, the one defined in the first parent class listed will be used.


class ParentClass1:
    def method1(self):
        print("This is method 1 of ParentClass1")

class ParentClass2:
    def method2(self):
        print("This is method 2 of ParentClass2")

class ChildClass(ParentClass1, ParentClass2):
    def method3(self):
        print("This is method 3 of ChildClass")

In this example, the ChildClass is inheriting from ParentClass1 and ParentClass2 . The ChildClass has its own method, method3() , but it can also use the methods of both parent classes, such as method1() and method2() .

Method Resolution Order (MRO)

When a class has multiple parent classes, Python determines the order in which it should search for a method or property by following a method resolution order (MRO). The MRO is determined by a depth-first search of the inheritance graph, from left to right. The leftmost parent class is searched first, followed by its parent classes, and so on.


class ParentClass1:
    def method1(self):
        print("This is method 1 of ParentClass1")

class ParentClass2:
    def method1(self):
        print("This is method 1 of ParentClass2")

class ChildClass(ParentClass1, ParentClass2):
    pass

obj = ChildClass()
obj.method1()

In this example, both ParentClass1 and ParentClass2 have a method called method1() , but the ChildClass is not overriding this method. When method1() is called on an object of ChildClass , it will use the implementation of method1() in ParentClass1 , since it is the leftmost parent class.

Best Practices

Multiple inheritance can be a powerful tool, but it can also lead to complex class hierarchies and name collisions if not used carefully. Here are some best practices to follow when using multiple inheritance in Python:

  • Prefer composition over inheritance: Sometimes, it may be better to use composition (creating a new class that contains instances of other classes) instead of inheritance, especially if you only need to use a few methods or properties from a parent class.
  • Avoid deep inheritance hierarchies: If you find yourself inheriting from multiple parent classes that also inherit from other classes, it may be a sign that your class hierarchy is becoming too complex.
  • Use super() to call parent methods: When you override a method in a derived class, you can call the implementation of the parent class using the built-in super() function.
  • Keep class interfaces consistent: If you're inheriting from multiple parent classes with similar methods, make sure that the method signatures (the arguments and return values) are consistent across all parent classes.

Conclusion

Multiple inheritance can be a powerful tool for creating complex class hierarchies and reusing code in Python, but it should be used with caution. By following best practices such as preferring composition over inheritance, avoiding deep inheritance hierarchies, and using super() to call parent methods, you can write more maintainable and flexible code.