SoFunction
Updated on 2025-05-07

Where to use multiple inheritance of Python

Preface

In Python, multiple inheritance is a powerful feature that allows a subclass to inherit properties and methods from multiple parent classes. However, multiple inheritance may also lead to some problems. In this article, we will explore the problems that multiple inheritance may bring.

Use multiple inheritance with caution

Why use it with caution? Let’s review a knowledge point, what is super() used for? In my impression, it is a tool function used to call the parent class method. Is that accurate? Let's look at a case:

class A:
    def __init__(self):
        print("A")
​
​
class B(A):
    def __init__(self):
        print("B")
        super().__init__()
​
​
class D(B):
    pass
​
D()

This is a simple single inheritance relationship, instantiating the D object, and outputs

B
A

It seems that it is correct. Super() is indeed calling the parent class method. Let's take a look at the case of multiple inheritance:

class A:
    def __init__(self):
        print("A")
​
​
class B(A):
    def __init__(self):
        print("B")
        super().__init__()
​
class C(A):
    def __init__(self):
        print("C")
        super().__init__()
​
class D(B, C):
    pass
​
D()
print(D.__mro__)  # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

This is a multiple inheritance, first look at the output result:

B
C
A

After we see the output B,B.__init__()Super() in the program does not find the parent class A of B to execute, but instead locates the next class C in the MRO chain.

All multiple inheritance is a complex feature, especially when using super(), because the method of the current class parent class is not called at this time. When you don't design a coverage relationship between good methods, use multiple inheritance with caution, and perhaps abstraction can solve your problem.

Mixin mode

The Mixin pattern is a software design pattern used to implement code reuse and composition in object-oriented programming. It allows some common functions to be independent and multiplexed by mixing them into different classes.

Mixin is a class that contains a set of methods and properties, which is not usually instantiated separately, but is used as part of other classes. By multiple inheritance of the Mixin class with other classes, methods and properties in Mixin can be merged into the target class, thereby enhancing the functionality of the target class.

The advantage of the Mixin pattern is that it improves the reusability and flexibility of the code. You can quickly build classes with different functional combinations by applying different Mixin combinations to different classes. Meanwhile, Mixin does not introduce tight coupling relationships, as it only provides some optional feature extensions.

Here is a simple Python example that demonstrates how to use the Mixin mode:

class LoggerMixin:
    def log(self, message):
        print(f"Log: {message}")
​
class User:
    def __init__(self, name):
         = name
​
class Admin(User, LoggerMixin):
    def __init__(self, name):
        super().__init__(name)
​
admin = Admin("John")
("Admin logged in")  # use LoggerMixin In-house log method

In the example above, LoggerMixin is a Mixin class that contains a log method. The Admin class obtains log methods from User and LoggerMixin through multiple inheritance, so that the log method can be called after instantiating the Admin object.

It should be noted that Mixin should only contain some common methods and properties, and should not depend on other methods and properties in the target class. Mixin's design principle is to maintain independence as much as possible so that it can be reused in different classes.

at last

Python multiple inheritance is a powerful feature, but it can also cause some problems. To avoid these problems, we should use multiple inheritance with caution. When implementing multiple inheritance, we can use the super() function, explicitly call the parent class method, and use Mixin and other technologies.

This is the article about the use of multiple inheritance of Python. For more related content on multiple inheritance of Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!