YouTip LogoYouTip

Python Multiple Inheritance

## Python Multiple Inheritance: A Comprehensive Guide In object-oriented programming, **Multiple Inheritance** is a feature that allows a class to inherit attributes and methods from more than one parent class. Python fully supports multiple inheritance. By using it, a child class can combine and reuse functionalities from multiple base classes, promoting modularity and code reuse. --- ## Syntax of Multiple Inheritance To create a child class that inherits from multiple parent classes, list the parent classes inside parentheses after the child class name, separated by commas. ```python class Parent1: # Attributes and methods of Parent1 pass class Parent2: # Attributes and methods of Parent2 pass class Child(Parent1, Parent2): # Attributes and methods of Child, plus inherited ones pass ``` --- ## Code Example Below is a practical example demonstrating how to define and instantiate a class with multiple inheritance. ```python # Define the first parent class class Parent1: def method1(self): print("This is method1 from Parent1") # Define the second parent class class Parent2: def method2(self): print("This is method2 from Parent2") # Define the child class inheriting from both Parent1 and Parent2 class Child(Parent1, Parent2): def method3(self): print("This is method3 from Child") # Create an instance of the Child class child = Child() # Call the method inherited from Parent1 child.method1() # Call the method inherited from Parent2 child.method2() # Call the Child class's own method child.method3() ``` ### Output ```text This is method1 from Parent1 This is method2 from Parent2 This is method3 from Child ``` ### Code Explanation * **`Parent1` and `Parent2`** are two independent base classes, defining `method1` and `method2` respectively. * **`Child`** is the derived class that inherits from both `Parent1` and `Parent2`. It also defines its own unique method, `method3`. * When we instantiate `Child`, the resulting object has access to all methods defined in `Parent1`, `Parent2`, and `Child`. --- ## Key Considerations & Best Practices While multiple inheritance is a powerful tool, it introduces architectural complexities that you should keep in mind: ### 1. Method Resolution Order (MRO) If multiple parent classes define a method with the same name, Python needs a way to decide which method to execute. Python resolves this using the **C3 Linearization** algorithm to construct the **Method Resolution Order (MRO)**. You can inspect the lookup order of any class using the `__mro__` attribute or the `mro()` method: ```python print(Child.__mro__) # Output: (, , , ) ``` Python searches for methods from left to right as specified in the class definition (`Child(Parent1, Parent2)`). ### 2. The Diamond Problem The "Diamond Problem" occurs when two subclassess (`B` and `C`) inherit from a superclass (`A`), and a fourth class (`D`) inherits from both `B` and `C`. ```text A / \ B C \ / D ``` Python solves this cleanly using MRO and the `super()` function. When designing complex hierarchies, always use `super()` instead of explicitly calling parent class names (e.g., `Parent1.__init__(self)`) to ensure that every base class constructor is called exactly once and in the correct order. ### 3. Use Mixins for Cleaner Architecture To avoid the pitfalls of deep multiple inheritance hierarchies, Python developers often use **Mixins**. A Mixin is a small, specialized class designed to provide specific, reusable behaviors to other classes without acting as a standalone parent class.
← Python Infinite SequencePython Classmethod β†’