Home  >  Article  >  Backend Development  >  super() method in Python

super() method in Python

不言
不言Original
2018-06-01 16:36:221579browse

This article mainly introduces the super() method in Python, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

super is used to solve multiple inheritance problems Yes, there is no problem when using single inheritance to directly call the parent class method using the class name, but if you use multiple inheritance, it will involve various problems such as search order (MRO) and repeated calls (diamond inheritance). This article mainly introduces you to the relevant information about the super() method in Python. Friends who need it can refer to it.

Preface

Python classes include new-style classes and classic classes, both of which support multiple inheritance. In class inheritance, if you want to override the parent class method instead of overriding the parent class method, at this time we can use the super() method to achieve

Python language and C have similar class inheritance , when the class is defined, the first self will be customized in Python, similar to the this pointer in C, pointing to the object itself.

Python simple class example:

>>> class hello(object): 
...     def print_c(): 
...       print"hello world!" 
>>> hello().print_c() 
hello world!

Of course, in practice, it is inevitable to need class inheritance. The class inherits the parent class, normally as follows:

>>> class child(hello): 
...     def print_c(self): 
...         hello().print_c() 
...          
>>> child().print_c() 
hello world!

The super() mechanism is also provided in python, the example is as follows:

>>> 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!

Note

Previous versions of Python2.2: Classic class era

A classic class is a class without inheritance. The instance types are all type types. If a classic class is used as a parent class, such an error will be returned when the subclass calls the constructor of the parent class'' TypeError: must be type, not classobj'''

At this time, the MRO method is DFS (depth-first search (child node order: left to right)). Therefore, new-style classes are used in this article, and the search algorithm for new-style classes is the C3 algorithm

class C(object):
 def minus(self,x):
  return x/2

class D(C):
 def minus(self,x):
  super(D, self).minus()
  print 'hello'

In the above code, C is the parent class, and D It is a subclass. We have redefined the minus method in class D, which is to add a new print 'hello' function based on the functions of class C. The role of super here is to call the method of the parent class in the subclass. This is also a common usage of calling super() in single inheritance. So here comes the question

class A(object):
 def __init__(self):
  self.n = 10

 def minus(self, m):
  self.n -= m


class B(A):
 def __init__(self):
  self.n = 7

 def minus(self, m):
  super(B,self).minus(m)
  self.n -= 2
b=B()
b.minus(2)
print b.n

So what is the output of b.n in the above code? Why is the result 2 instead of 5? super(B,self).minus(m) obviously calls the minus method of the parent class, but the output result is 2. You need to understand that the current instance of B, not the instance of A, then the value of self.n passed It is 7, not 10.

So how does super work when there is multiple inheritance? Come on, now create a class C that inherits A, and then create a class D that inherits B and C, and see how to call the super overridden method.

class C(A):
 def __init__(self):
  self.n = 12

 def minus(self, m):
  super(C,self).minus(m)
  self.n -= 5


class D(B, C):
 def __init__(self):
  self.n = 15

 def minus(self, m):
  super(D,self).minus(m)
  self.n -= 2

d=D()
d.minus(2)
print d.n

What is the output result of the above code? Don't be impatient, just see how it works first. As mentioned above, the new class uses the C3 algorithm when searching for child nodes. So how is it found? D->B->C->A->object. How can I verify that this order is correct?

D.__mro__
(<class &#39;__main__.D&#39;>, <class &#39;__main__.B&#39;>, <class &#39;__main__.C&#39;>, <class &#39;__main__.A&#39;>, <type &#39;object&#39;>)

What is Mro? For each class you define, Python will calculate a method resolution order (MRO) list, which represents the order of class inheritance.

Related recommendations:

Detailed explanation of the usage and working principle of the super() function in python

##

The above is the detailed content of super() method in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn