A brief analysis of multiple inheritance in Python

Source: Internet
Author: User
Tags inheritance require in python

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:

Dog-Dog;

Bat-bat;

Parrot-Parrot;

Ostrich-Ostrich.

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.

Mixin

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.

Summary

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.