Understand multiple inheritance and Mixin design patterns in Python

Single inheritance of classes is familiar to us, and it is effortless to write. As for multiple inheritance, there are many and very few written. In many project codes, you will also see a very strange class. They have one common point in naming, that is, they like to use Mixin at the end of the class name.

1. Know the Mixin mode

So let's talk about this Mixin today. How do we understand this Mixin? It is actually a design pattern. If there is no consensus on such a design pattern among developers, then the design pattern will no longer exist.

In order for everyone to have a more intuitive understanding of this Mixin, I have excerpted an explanation from the Internet.

Inheritance is an "is-a" relationship. For example, the car class inherits the vehicle class because the car is an ("is-a") vehicle. An item cannot be many different things, so there should be no multiple inheritance. But is there such a situation, a class does need to inherit multiple classes?

The answer is yes. Let's take transportation as an example. Civil aviation aircraft is a type of transportation. For local tyrants, helicopters are also a type of transportation. For these two vehicles, they both have a function of flying, but cars do not. Therefore, it is impossible for us to write the flight function in the parent class of transportation. However, if both civil aviation aircraft and helicopters write their own flying methods, it violates the principle of reusing the code as much as possible (if there are more and more flying tools in the future, there will be a lot of duplicate code).

What to do, then let these two kinds of aircraft inherit the two parent classes of vehicle and aircraft at the same time, so that multiple inheritance occurs. At this time, it violates the inheritance must be an "is-a" relationship. How to solve this problem?

At this time Mixin is on the stage. Flight is just an (enhanced) attribute of the aircraft as a vehicle. We can define a separate (enhanced) class for this flying function, which is called the Mixin class. This class is added to the subclass as an enhanced function. In order to let other developers know that this is a Mixin class at first glance, developers are generally required to follow the specification and add Mixin at the end of the class name.

for example

class Vehicle(object):
    pass

class PlaneMixin(object):
    def fly(self):
        print('I am flying')

class Airplane(Vehicle, PlaneMixin):
    pass

Use Mixin class to implement multiple inheritance to follow the following specifications

  • Responsibility is clear: must express a certain function, not a certain item;
  • Single function: if there are multiple functions, write multiple Mixin classes;
  • Absolutely independent: cannot rely on the implementation of the subclass; even if the subclass does not inherit the Mixin class, it can still work, but it lacks a certain function.

2. Disadvantages of not using Mixin

You will definitely ask, can you not use Mixin?

Of course, this question is like asking, do I not follow the PEP8 code specification? No problem at all, it's just not recommended.

So what reason is there for us to use Mixin design pattern?

If not used, there are probably three disadvantages as follows:

1. Complex structure

What is the parent class of a class in single inheritance, and what is the parent class of the parent class is very clear. Inheritance of a class has multiple parent classes, and the parent class has multiple parent classes, and the inheritance relationship is complicated.

2. The priority is fuzzy

There are methods with the same name in multiple parent classes. During the development process, it is easy to cause confusion. The child class does not know which parent class to inherit, which will increase the difficulty of development. Regarding the inheritance order of subclasses, there is a more complicated C3 algorithm. If you are still unclear, you can click on my other article to find out.

3. Function conflict

Multiple inheritance has multiple parent classes, but subclasses can only inherit one. For methods with the same name, the methods of the other parent class will fail.

Welfare at the end of the sentence

My original book " PyCharm Chinese Guide " was released some time ago, and it became popular in the entire Python circle. In just one day of release, the downloads exceeded 1,000, and on the same day, it received hundreds of stars on Github. Up to now, the number of downloads has exceeded 10,000.

This book has a total of nearly 200 pages , contains a large number of illustrations , and is well-made , worthy of every Python engineer.

For your convenience, I uploaded this book to Baidu Netdisk, you can get it yourself.

Link: https://pan.baidu.com/s/1-NzATHFtaTV1MQzek70iUQ

Password: mft3

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/108976547