In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the python super () function". In daily operation, I believe many people have doubts about how to use the python super () function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the python super () function". Next, please follow the editor to study!
Python is an object-oriented language, and inheritance is often used when defining a class. In class inheritance, the subclass inherits the encapsulated method in the parent class and does not need to be written again. If the subclass redefines a method of the parent class, then the method will overwrite the method of the parent class, but sometimes we want the subclass to extend on the basis of the parent class method instead of directly overriding it. You need to call the method of the parent class first, and then extend the function, and then you can call the method of the parent class through super.
The usage of super
Look at the following example:
Class A: def func (self): print ("func execution of A") class B (A): def func (self): super () .func () print ("func execution of B extension") b = B () b.func ()
# the output is as follows:
# func execution of A
# func execution of B extension
In the above program, An is the parent class and B is a subclass of A. We redefine the func () method in Class A, redefine the func () method in Class B, and call the method of the parent class through super (). Func () in the method, so the execution result will have the output of Class A func () method.
If you often look at the source code of Python built-in libraries and third-party libraries, you will find that super uses a lot of places to call the initialization _ _ init__ () method of the parent class in the subclass, which is very common.
Class A: def _ init__ (self, x): self.x = xclass B (A): def _ init__ (self, x, y): super (). _ init__ (x) self.y = yb = B (1,2) print (b.x, b.y)
Seeing this, you will think that super is used to get the parent class and call the parent class method. It is actually wrong to say that supper is not the parent class, but the next class in the MRO list. The so-called MRO list is the method parsing order (Method Resolution Order) list, which represents the order in which the class inherits. We can use the following ways to get the MRO list of a class:
C.mro () C.The classrooms of the classrooms are the same.
The determination of the order of the MRO list has undergone many changes, and the latest one is achieved through the C3 linearization algorithm. If you are interested, you can find out for yourself. Generally speaking, the MRO list of a class is to merge the MRO lists of all the parent classes and follow the following three principles:
The subclass is always in front of the parent class
If there are multiple parent classes, they are checked according to their order in the list
If there are two legal choices for the next class, select the first parent class
Let's take a look at this example:
Class A (Base): def func (self): print ("func execution of A") super (). Func () print ("func execution of A") class B (Base): def func (self): print ("func execution of B") super (). Func () print ("func execution of B") class C (A) B): def func (self): print ("func execution of C") super () .func () print ("func execution of C") c = C () c.func () # get the MRO list print (c.
The implementation results are as follows:
In the above program, Base is the parent class, and An and B both inherit from Base,C and inherit from An and B. their inheritance relationship is a typical diamond inheritance, as follows:
From the results, we can see that super is not a method to get the parent class and use to call the parent class, but to call the next class at a time according to the MRO list. You can get the MRO list using c. MRO list. The order of the MRO list is C, A, B, Base, object.
The principle of super
The super calculation method parses the next class in the order and can receive two parameters:
Def super (cls, inst): mro = inst.__class__.mro () return mro [mro.index (cls) + 1] at this point, the study on "how to use the python super () function" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.