Python Multiple Inheritance


Multiple Inheritance in Python

Multiple inheritance is a feature in object-oriented programming (OOP) that allows a class (the child class or subclass) to inherit attributes and methods from more than one parent class (the base classes or superclasses). This enables the child class to combine functionalities from multiple sources, promoting code reuse and extensibility.

Key Features of Multiple Inheritance

  1. Multiple Parent Classes: A single child class can inherit from multiple parent classes, allowing it to access and utilize the properties and methods of all its parents.

  2. Method Resolution Order (MRO): When a method is called on an instance of the child class, Python uses a specific algorithm (C3 linearization) to determine the order in which classes are searched for the method. This is important for resolving conflicts when methods with the same name exist in multiple parent classes.

  3. Code Reusability: Similar to single inheritance, multiple inheritance allows for greater code reuse by combining functionalities from different classes.

  4. Potential for Complexity: While multiple inheritance can be powerful, it can also introduce complexity, especially when dealing with method conflicts or diamond problems (when two parent classes inherit from the same base class).

Example of Multiple Inheritance

Here’s an example to illustrate multiple inheritance in Python:

# Parent class 1 class Vehicle: def start(self): return "Vehicle started" # Parent class 2 class Engine: def start_engine(self): return "Engine started" # Child class inheriting from Vehicle and Engine class Car(Vehicle, Engine): def drive(self): return "Car is driving" # Creating an instance of Car my_car = Car() # Accessing methods from both parent classes print(my_car.start()) # Output: Vehicle started print(my_car.start_engine()) # Output: Engine started print(my_car.drive()) # Output: Car is driving

Explanation of the Example

  1. Parent Classes:

    • Vehicle: This class has a method start() that simulates starting a vehicle.
    • Engine: This class has a method start_engine() that simulates starting an engine.
  2. Child Class (Car):

    • The Car class inherits from both Vehicle and Engine, meaning it has access to the methods from both parent classes.
    • It also has its own method drive() to simulate driving the car.
  3. Creating an Instance: An instance of the Car class is created.

  4. Method Access: The Car instance can call methods from both parent classes (start() from Vehicle and start_engine() from Engine) as well as its own method drive().

Method Resolution Order (MRO)

In multiple inheritance, if there are conflicting method names in parent classes, Python uses the Method Resolution Order (MRO) to determine which method to call. You can view the MRO of a class using the mro() method or the __mro__ attribute.

Example:

print(Car.mro()) # Output: [<class '__main__.Car'>, <class '__main__.Vehicle'>, <class '__main__.Engine'>, <class 'object'>]

Diamond Problem

Multiple inheritance can lead to the diamond problem, where two parent classes inherit from the same base class, creating ambiguity about which method to call. Python resolves this using its MRO.

Example:

class A: def show(self): return "Class A" class B(A): def show(self): return "Class B" class C(A): def show(self): return "Class C" class D(B, C): pass obj = D() print(obj.show()) # Output: Class B (B is before C in MRO)

Summary

  • Multiple inheritance allows a child class to inherit from more than one parent class, providing the ability to combine functionalities from different classes.
  • The Method Resolution Order (MRO) helps determine which method to invoke in case of name conflicts.
  • While it offers flexibility and code reuse, multiple inheritance can introduce complexity and ambiguity, particularly with method conflicts or the diamond problem.

If you have any further questions or need more examples, feel free to ask!