Methods for accessing super-class Python Advanced Programming: Super ()

Source: Internet
Author: User


# python:2.x

__author__ = ' Administrator '


#它是一个内建类型 for accessing superclass attributes belonging to an object

Print Super#<type ' super ' >

#如果已习惯于通过直接调用父类将self作为第一参数来访问的特性, there may be confusion


Class M (object):

def s (self):

Print U ' is not a superclass method '

Class S (M):

def s (self):

M.S (self)

Print U ' just inheriting '

A=s ()

A.S ()


Not a super-class method

It's just a succession.


#看下M. S (self) line, call the S method in the superclass (M class) using the method just described, and pass self as the first parameter, which means that the S () method that belongs to M is called, but the instance that invokes it returns self


Class S1 (M):

def s (self):

Super (S1,self). S ()

Print U ' using Super Class super () '

#注意: When multi-inheritance mode, super () will become difficult to use, you should understand the method parsing order (MRO)

#理解python方法解析顺序 (MRO)


The MRO ( built for Dylan was added in 2.3, the new MRO C3

The reference document written by Michele Simionato can be found on the WWW.PYTHON.ORG/DOWNLOAD/RELEASES/2.3/MRO, which describes the linearization of the C3 building a class (priority, a sort of sequence list of ancestors)

This list is used to find the attributes

The changes to the MRO are used to solve the problems introduced by creating a common basic type (object), and if a class has 2 ancestors before it becomes a C3 linear method, the MRO calculation will be simple as follows:



Class C1:pass

Class C2:

def m (self):

print ' C1+C2 '

Class C3 (C1,C2):p

C3=C3 ()

C3.M () #C1 +C2

#当c3. M is called, the interpreter will find the C3 method, then find it in C1, and finally find it in C2


Refer to the Basebase class on top of 2 base classes (c1,c2 inherit from it,:)

As a result, the old MRO, based on the rule "left to right depth first", finds in C2 will be returned to the top by the C1 class

Look at the code below and there's an odd situation.


Class Basebase:

def methond (self):

print ' Basebase '

Class B1 (Basebase):


Class B2 (Basebase):

def methond (self):

print ' B2 '

Class MB (B1,B2):


A=MB ()

A.methond () #BaseBase


This inheritance is rarely seen, so this is more of a theoretical issue than a practice issue, and the standard library does not build such an inheritance hierarchy, but because of the introduction of object at the top of the type hierarchy, the C-side (c-side) suddenly has multiple inheritance problems, causing a conflict in the subtype

, because the use of the existing MRO to make his normal work to spend more energy, so proposed a new MRO



Class A (object):

def m (self):

print ' A '

Class A1 (A):


Class A2 (A):

def m (self):

print ' A2 '

Class A3 (A1,A2):


A=A3 ()

a.m. () #A2

# (object) The new MRO is based on a recursive invocation above a base class, as follows: * * (Focus: L[A3 (A1,A2)]=a3+merge (L[A1],L[A2],A1,A2))


L[A3] is the linearization of the A3 class, and merge is a concrete algorithm for merging multiple linearization results

The linearization of C is the sum of the C plus the parent class's linearization and the parent class list merge

The merge algorithm is responsible for deleting duplicates and maintaining the correct order, which is described in the article as

Take the head of the first list, that is, l[a1][0], if it is not at the end of any table, add it to the A3 linearization and remove from the merge list, otherwise find the header of the next list, if it is a good table header

Then take it

Then repeat the operation until all classes are deleted or you cannot find a good header, in the example above, it is impossible to build a merge, 2.3 will reject the creation of the A3 class will throw an exception

A (header) is the first element of the list, while tail (footer) contains the rest of the elements, such as in the (A1,a2,an ... ), A1 is the table header, (A2,...., an) is the footer

In other words, C3 the recursive attempt to find the list order on each parent class, and then, when a class involves multiple lists, computes a left-to-right rule that uses hierarchy ambiguity elimination to merge all the lists

As follows:


def L (K):

return [k.__name__ for K in k.__mro__]

Print L (A3) #[' A3 ', ' A1 ', ' A2 ', ' A ', ' object ']



The _mro_ of a class is used to store linearized calculations, and calculations are completed when the class definition is loaded

It is also possible to call A3.MRO () to calculate and obtain the result

Reminder: This works only for new style classes, so it's not a good way to mix new and old forms of classes in the codebase, and MRO performance will be different


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: 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.