In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what are the application scenarios of Python programming super", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the scenarios of Python programming super applications?"
Catalogue
What is super
Third, the common use scenarios of super
What is super
1.super is also a class, yes. It is neither a method nor a built-in keyword.
Class A: passprint (type (super (A)
Output result
By looking directly at the source code of super, you can also see that it is a class
In addition, many articles on the Internet explain that super is used to call parent methods, which is also a wrong view!
Let's say we have the following example:
Class A: def _ init__ (self): print ("A") class B (A): def _ init__ (self): print ("B") super (). _ _ init__ () class C (A): def _ init__ (self): print ("C") super (). _ init__ () class D (B) C): def _ init__ (self): print ("D") super (). _ _ init__ () D ()
According to the understanding that "super is used to call the method of the parent class", the execution process of the above code should be:
Print ("D")-[calling super will execute B and C successively]-execute B:print ("B") first-[call super to execute A]-
Print ("A")-[calling super will execute B and C successively]-execute C:print ("C") after calling super-
Print ("A")
The theory of implementation result should be: D B A C A
But this is not the case!
Actual implementation result
D
B
C
A
So it is wrong to say that "super is used to call the methods of the parent class"!
In fact, the call to super follows Python's [MRO (method parsing order)]. In Python3, MRO is implemented based on C3 algorithm.
You can refer to this article http://kaiyuan.me/2016/04/27/C3_linearization/ for an explanation of MRO and C3 algorithms.
Third, the common use scenarios of super
1. If multiple parent classes we inherit have methods with the same name, we can use super to specify which parent class's method to use
Class A: def test (self): print ('A') class B: def test (self): print ('B') class C (A, B): def _ init__ (self): super (). Test () # call test method super (C, self). Test () # call test method super (A) in Class A Self) .test () # call the test method C () in class B
Output result
A
A
B
two。 When we use a method of the parent class in a subclass and want to extend it without completely rewriting it, we can use super () to implement incremental modifications to the method:
For example, what should we do if we want to change the method of append in list to Chinese addition?
And there is no return value when list calls the append method in python. What should we do if we want to add a return value after the element operation is successful?
First, let's look at the execution result by calling the original list.
A=list () res=a.append (1) print (res)
Output result
None
You can see that the value returned after calling the append method is None
Now let's rewrite it through super so that it has a return value and can call append directly in Chinese:
Class list (list): def add (self, * args, * * kwargs): super () .append (* args, * * kwargs) return "added successfully" x = list () res = x. Add (1) print (res) print (x)
Output result
Added successfully
[1]
Super is actually quite common. For example, in restfremework, when you need to rewrite the Response information of its response result, you can also use super to rewrite its dispatch in addition to the middleware implementation of django.
At this point, I believe you have a deeper understanding of "Python programming super application scenarios". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.