This article mainly introduces the multiple inheritance in Python, is the basic knowledge of Python learning, code based on python2.x version, the need for friends can refer to the
Inheritance is an important way of object-oriented programming, because subclasses can extend the functionality of a parent class by inheriting it.
Recall the animal class design, assuming that we want to achieve the following 4 kinds of animals:
If we classify mammals and birds, we can design the hierarchy of such classes:
But if you classify it as "can run" and "fly," we should design the hierarchy of such classes:
If you want to include both categories, we have to design more layers:
Mammals: can run the mammals, can fly the mammals;
Bird: A bird that can run, a bird that can fly.
In this way, the class hierarchy is complex:
If we want to increase the "pet" and "non-pet", so that the number of classes will increase exponentially, it is obvious that this design is not.
The right approach is to use multiple inheritance. First, the main class levels are still designed according to mammals and birds:
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22||Class Animal (object): Pass # Large Class: Class mammal (Animal): Pass class Bird (Animal): Pass # All kinds of Animals: class Dog (mammal): Pass Class Bat (mammal): Pass class Parrot (Bird): Pass class Ostrich (Bird): Pass|
Now, to add runnable and flyable functions to animals, we need to first define the runnable and flyable classes:
|1 2 3 4 5 6 7||Class Runnable (object): Def run (self): print (' Running ... ') class flyable (object): Def Fly (self): print (' flying ... ')|
For animals that require runnable functions, they inherit a runnable, such as dog:
|1 2||Class Dog (Mammal, Runnable): Pass|
For animals that require flyable functions, inherit a flyable, such as Bat:
|1 2||Class Bat (Mammal, flyable): Pass|
With multiple inheritance, a subclass can get all the features of multiple parent classes at the same time.
When designing an inheritance relationship for a class, the mainline is usually inherited by a single, for example, ostrich inherits from Bird. However, if you need to "mix" additional functionality, through multiple inheritance can be implemented, for example, let ostrich in addition to inherit from Bird, and then inherit runnable. This design is often called mixin.
To better see the inheritance relationship, we changed runnable and flyable to Runnablemixin and Flyablemixin. Similarly, you can define carnivorous carnivorousmixin and animal herbivoresmixin, allowing an animal to have several mixin at the same time:
|1 2||Class Dog (mammal, Runnablemixin, carnivorousmixin): Pass|
The goal of Mixin is to add multiple functions to a class, so that when designing a class, we prioritize multiple mixin through multiple inheritance rather than designing multiple, complex inheritance relationships.
A lot of Python's own libraries also use mixin. Python, for example, has two types of network services, TCPServer and Udpserver, and a multiple-process or multithreaded model for serving multiple users, both of which are provided by Forkingmixin and ThreadingMixIn. By combining, we can create the right service.
For example, a TCP service that writes a multi-process pattern is defined as follows:
|1 2||Class Mytcpserver (TCPServer, forkingmixin): Pass|
Write a multithreaded mode of UDP service, defined as follows:
|1 2||Class Myudpserver (Udpserver, threadingmixin): Pass|
If you're going to get a more advanced coprocessor model, you can write a coroutinemixin:
|1 2||Class Mytcpserver (TCPServer, coroutinemixin): Pass|
In this way, we do not need a complex and large inheritance chain, as long as we choose to combine the functions of different classes, you can quickly construct the required subclasses.
Because Python allows multiple inheritance, mixin is a common design.
Only a single inherited language, such as Java, is not allowed to use the mixin design.