Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of super method in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "the use of super method in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of super method in Python"!

Preface

The initial use of super ()

In class inheritance, if you redefine a method, the method overrides the method of the parent class with the same name, but sometimes we want to implement the function of the parent class at the same time, so we need to call the method of the parent class, which can be done by using super, such as:

Class Animal (object): def _ init__ (self, name): self.name = name def greet (self): print 'Hello, I am% s.'% self.name class Dog (Animal): def greet (self): super (Dog, self). Greet () # Python3 can use super (). Greet () print' WangWang...'

Above, Animal is the parent class and Dog is the subclass. We redefine the greet method in the Dog class. In order to implement the function of the parent class at the same time, we call the method of the parent class. See the following usage:

> dog = Dog ('dog') > dog.greet () Hello, I am dog.WangWang..

One of the most common uses of super is to call the initialization method of the parent class in a subclass, such as:

Class Base (object): def _ init__ (self, a, b): self.a = a self.b = b class A (Base): def _ init__ (self, a, b, c): super (A, self). _ init__ (a, b) # Python3 can use super (). _ init__ (a, b) self.c = c to drill down into super ()

Looking at the above usage, you may think that the use of super is very simple, which is nothing more than getting the parent class and calling the method of the parent class. In fact, in the above case, the class obtained by super happens to be the parent class, but not necessarily in other cases. Super actually has no substantial relationship with the parent class.

Let's look at a slightly more complex example that involves multiple inheritance, with the following code:

Class Base (object): def _ init__ (self): print "enter Base" print "leave Base" class A (Base): def _ init__ (self): print "enter A" super (A, self). _ init__ () print "leave A" class B (Base): def _ init__ (self): print "enter B" super (B) Self). _ init__ () print "leave B" class C (A, B): def _ _ init__ (self): print "enter C" super (C, self). _ init__ () print "leave C"

Where Base is the parent class, An and B inherit from Base, and C inherits from An and B. their inheritance relationships are as follows:

Base /\ / A B\ / C

Now, let's take a look at using:

> c = C () enter Center Aenter Benter Baseleave Baseleave Bleave Aleave C

If you think that super stands for "calling the method of the parent class", you are probably wondering why the next sentence of enter An is not enter Base but enter B. The reason is that there is no substantial relationship between super and the parent class, so let's figure out how super works.

MRO list

In fact, for each class you define, the Python accountant calculates a method parsing order (Method Resolution Order, MRO) list, which represents the order in which the class inherits. We can get the MRO list of a class in the following ways:

> C.mro () # or C.roommrology _ or C (). _ _ class__.mro () [_ _ main__.C, _ _ main__.A, _ _ main__.B, _ _ main__.Base, object]

So how is the order of the MRO list determined? it is realized through a C3 linearization algorithm. Let's not delve into this algorithm. Interested readers can learn about it for themselves. 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

Super principle

Super works as follows:

Def super (cls, inst): mro = inst.__class__.mro () return mro [mro.index (cls) + 1]

Where cls represents the class and inst represents the instance, the above code does two things:

Get the MRO list of inst

Find the index of cls in the current MRO list and return its next class, mro [index + 1]

When you use super (cls, inst), Python will search inst's MRO list for the next class of cls.

Now, let's go back to the previous example.

First, take a look at the init method of class C:

Super (C, self). _ _ init__ ()

The self here is an instance of the current C, and the result of self.class.mro () is:

[_ _ main__.C, _ _ main__.A, _ _ main__.B, _ _ main__.Base, object]

As you can see, the next class of C is A, so when you jump to the init of A, enter An is printed and the following line of code is executed:

Super (A, self). _ _ init__ ()

Notice that the self here is also an instance of the current C, and the MRO list is the same as above. Search for the next class of An in MRO and find that it is B. therefore, if you jump to the init of B, enter B will be printed instead of enter Base.

The whole process is clear, and the key is to understand how super works, rather than taking it for granted that super calls the methods of the parent class.

Summary

In fact, there is no substantial relationship between super and the parent class.

Super (cls, inst) gets the next class of cls in inst's MRO list.

At this point, I believe you have a deeper understanding of "the use of the super method in Python". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report