Python multi-inheritance instance and python instance

Source: Internet
Author: User
Tags what inheritance

Python multi-inheritance instance and python instance

The examples in this article describe how to use multiple inheritance in python and share them with you for your reference. The specific implementation method is as follows:

1. the mro. py file is as follows:

#!/usr/bin/python# Filename:mro.py class P1:  def foo(self):    print 'called P1-foo' class P2:  def foo(self):    print 'called P2-foo'   def bar(self):    print 'called P2-bar' class C1(P1, P2):  pass class C2(P1, P2):  def bar(self):    print 'called C2-bar()' class GC(C1, C2):  pass

2. The execution result is as follows:

>>> from mro import *>>> gc = GC()>>> gc.foo()called P1-foo>>> gc.bar<bound method GC.bar of <mro.GC instance at 0xb77be2ac>>>>> gc.bar()called P2-bar>>>

3. Conclusion:

Method interpretation sequence (MRO): depth first, from left to right

I hope this article will help you with Python programming.


How to create multiple Python inheritance Solutions

However, when a project reaches a certain level of complexity, the Process Code usually exposes its fundamental defects. Next let's go directly to the object-oriented version of the previous example and see what changes have taken place. #! /Usr/bin/env python from subprocess import Popen, PIPE import re class DiskMonitor (): "" Disk Monitoring Class "def _ init _ (self, pattern = "2 [0-9] %", message = "capacity warning", cmd = "df-h"): self. pattern = pattern self. message = message self. cmd = cmd def disk_space (self): "" Disk space capacity flag method "" ps = Popen (self. cmd, shell = True, stdout = PIPE, stderr = PIPE) output_lines = ps. stdout. re Adlines () for line in output_lines: lineline = line. strip () if re. search (self. pattern, line): print "% s" % (self. message, line) class MyDiskMonitor (DiskMonitor): "" Customized Disk Monitoring Class "def disk_space (self): ps = Popen (self. cmd, shell = True, stdout = PIPE, stderr = PIPE) print "raw disk report:" print ps. stdout. read () if _ name _ = "_ main _": d = MyDiskMonitor () d. disk_space () Like the version, you can see that the code is more abstract. Sometimes too many abstractions can cause design problems, but in this example, it allows you to separate the problem into more reusable parts. The DiskMonitor class has _ init _ method. You can define new parameters in the DiskMonitor class, And the disk_space function is now a method in the class. With this new style, you can easily reuse and customize each part without changing the original code. When using process code, you usually have to change the original code. The multi-inheritance of Python is a more powerful and usually over-used aspect of object-oriented design. Inheritance allows you to reuse and customize existing code in the new class. Let's look at what inheritance may look like in the next example. This output differs greatly from the previous labeled version because it only uses the print statement injected at the top to print the unfiltered df-h command results. By Rewriting the methods in the MyDiskMonitor class, you can completely change the intent of the disk_space method. Introduction to the Python interpreter describes how to create a Python multi-threaded Environment Analysis of the Python multi-threaded problem how to establish a Python main thread? Execute the Python interpreter instructions to allow you to reuse ...... the remaining full text>

I. C language multi-inheritance instance and an instance that uses JAVA single-Inheritance mechanism to implement multi-inheritance (if the answer is satisfactory, I will add a score)

Interface C {
Void print1 ();
}
Interface D {
Void print2 ();
}
Interface E {
Void print3 ();
}
Interface F {
Void print4 ();
}
Interface G {
Void print5 ();
}

Class {
Void print0 (){
System. out. println ("00000000 ");
}
}
/* This is the implementation of java. I only know that C ++ can inherit more directly. The C language is not object-oriented programming. The following is the implementation of the java language I have provided for your reference !!
*/
Class B extends A implements C, D, E, F, G {
// Implement multiple inheritance through the implementation Interface
Void print1 (){
Print0 (); // inherit the method of Class
.....
}
Void print2 (){
.....
}
Void print3 (){
.....
}

Void print3 (){
.....
}
Void print4 (){
.....
}
Void print3 (){
.....
}
Void print4 (){
.....
}
Void print5 (){
.....
}

}

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.