Python Learning note __7.3 multiple inheritance

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial


A child class can have more than one parent class. This is called multiple inheritance. with multiple inheritance, a subclass can get all the functionality of multiple parent classes at the same time.

1.1 , M ixin

When you design an inheritance relationship for a class, the main line is usually single-inherited.

add extra functionality to a class through multiple inheritance. This design we call Mixin

    1. definition Mammal class

Class mammal (Object):

Pass

    1. definition runable class

Class Runnable (object):

def run (self):

Print (' Running ... ')

    1. definition Dog class, multiple inheritance

Class Dog (Mammal, Runnable):

Pass

Note:

    • In order to better see the inheritance relationship, we can change the runnable to Runnablemixin

    • Only allow single-inherited languages (such as Java) to use the mixin design

1.2 , Mixin Case

Python comes with two types of network services, TCPServer and Udpserver, and a multi-process or multithreaded model must be used to serve multiple users at the same time, both of which are forkingmixin and threadingmixin provided. By combining, we can create the right services to

    • Forkingminin : Multi-process

    • threadingmaxin : Multithreading

    1. Writing a TCP service with a multi-process pattern

Class Mytcpserver (TCPServer, forkingmixin):

Pass

    1. Write a multithreaded mode UDP service

Class Myudpserver (Udpserver, ThreadingMixIn):

Pass

    1. Co-process Model

Class Mytcpserver (TCPServer, coroutinemixin):

Pass


Python Learning note __7.3 multiple inheritance

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.