The super () method in Python

Source: Internet
Author: User
This article mainly introduces the super () method in Python, has a certain reference value, now share to everyone, the need for friends can refer to

Super is used to solve multiple inheritance problems, it is not a problem to call the parent class directly with the class name when using single inheritance, but if multiple inheritance is used, it involves various problems such as lookup order (MRO), repeated invocation (Diamond inheritance). This article mainly introduces you to the super () method in Python related information, the need for friends can refer to.

Objective

Python classes have new and classic classes, each supporting multiple inheritance. In class inheritance, if you want to override the parent class method instead of overriding the parent class method, we can use the super () method to implement the

The Python language has similar class inheritance to C + +, in which Python customizes the first self, like the this pointer in C + +, to the object itself.

Examples of simple Python classes:

>>> class Hello (object): ...     Def print_c (): ...       print "Hello world!" >>> hello (). Print_c () Hello world!

Of course, in practice, the inevitable need for class inheritance, subclasses inherit the parent class, normal as follows:

>>> class Child (Hello): ...     def print_c (self): ...         Hello (). Print_c ()          ... >>> Child (). Print_c () Hello world!

The super () mechanism is also available in Python, as in the following example:

>>> class Hello (object): ...     def print_c (self): ...       print "Hello world!"        ... >>> class Child (Hello): ...     def print_c (self): ...         Super (Child,self). Print_c ()          ... >>> Child (). Print_c () Hello world!

Attention

Python2.2 Previous version: Classic class era

The classic class is a class that does not inherit, the instance type is type, and if the classic class is used as the parent class, the subclass calls the constructor of the parent class and returns the error ' Typeerror:must be type, not classobj '

At this point the MRO method is DFS (depth-First search (child node order: left-to-right)). So the new class is used in this paper, and the search algorithm of the new class is the C3 algorithm.

Class C (object): Def minus (self,x):  return X/2class D (C): Def minus (self,x):  super (D, self). minus ()  print ' Hello

In the above code, C is the parent class, D is a subclass, we redefine the minus method in Class D, which is the new print ' Hello ' function based on the function of Class C. Super's role here is to invoke the parent class's method in the subclass, which is also the use of the common invocation of super () in single inheritance. So here's the problem.

Class A (object): Def __init__ (self):  SELF.N = ten def minus (self, m):  SELF.N-= MClass B (A): def __init__ (self): 
  SELF.N = 7 def minus (self, m):  super (B,self). Minus (m)  SELF.N-= 2b=b () b.minus (2) Print B.N

So what is the output of B.N in the code above? Why is the result 2 instead of 5? Super (B,self). Minus (m) is obviously called the parent class minus method, but the output is 2, is that you want to understand the instance of B now, rather than an instance of a, then the value of the SELF.N passed is 7, not 10.

So how does super work when it comes to multiple inheritance? , now create a class C that inherits a, and then create a class D that inherits B,c to see how super is overridden.

Class C (A): def __init__ (self):  SELF.N = def minus (self, m):  super (C,self). Minus (m)  SELF.N-= 5class D (B, C ): Def __init__ (self):  SELF.N = def minus (self, m):  super (D,self). Minus (m)  SELF.N-= 2d=d () D.minus (2) Print D.N

What is the result of the code output as above? Don't be impatient, let's see how it works. It is mentioned above that the C3 algorithm is used when looking for sub-nodes in the new class. So how does it look? D->b->c->a->object. How can you verify that the order is right?

d.__mro__ (<class ' __main__. D ';, <class ' __main__. B ';, <class ' __main__. C ';, <class ' __main__. A ';, <type ' object ' >)

What is MRO? For each class you define, Python calculates a list of method parsing orders (methods Resolution Order, MRO) that represents the order in which classes inherit.

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.