This is the 24th day of my participation in the August More Text Challenge

Python is an object-oriented language, and classes are often defined using inheritance. In class inheritance, a subclass inherits a method that has already been wrapped in the parent class and does not need to be written again. If a subclass redefines a method of the parent class, that method overwrites the method of the same name. But sometimes we want the subclass to keep the method of the parent class on the basis of extension, rather than directly overwrite, we need to call the method of the parent class first, and then extend the function. In this case, we can use super to realize the call of the method of the parent class.

The use of the super

Here’s an example:

class A:
    def func(self) :
        print("Func execution of A")


class B(A) :

    def func(self) :
        super().func()
        print("B extended func execution")


b = B()
b.func()
The output is as follows:
# A func execution
# B extended func execution
Copy the code

In the above program, A is the parent class, B is A subclass of A, we redefine the func() method in class A, redefine the func() method in class B, and call the superclass method in the method through super().func(), so the execution result will have the output of class A func() method.

If you look at Python’s built-in libraries and third-party library source code, you’ll see that the most common use of super is to call the parent class’s initializing __init__() method in a subclass.

class A:
    def __init__(self, x) :
        self.x = x

class B(A) :

    def __init__(self, x, y) :
        super().__init__(x)
        self.y = y
    

b = B(1.2)
print(b.x, b.y)
Copy the code

Super is used to fetch the superclass and call the superclass methods. Supper is not the superclass, but the next class in the MRO list, which is the Method Resolution Order list. It represents the order of class inheritance, and we can use the following methods to get a list of MROs for a class:

C.mro()
C.__mro__
c.__class__.__mro__
Copy the code

The order of MRO lists has undergone many changes, the most recent is realized by the C3 linearization algorithm, if you are interested in it, in general, the MRO list of a class is the MRO list of all parent classes, and follows the following three principles:

  • The subclass always precedes the parent class
  • If there are more than one parent class, it is checked based on the order in which they are in the list
  • If there are two legal choices for the next class, select the first parent class

Take a look at the following example:

class A(Base) :
    def func(self) :
        print("Func execution of A")
        super().func()
        print("Func of A completed")


class B(Base) :
    def func(self) :
        print("B funC execution")
        super().func()
        print("Func of B completed")

class C(A, B) :
    def func(self) :
        print("C func execution")
        super().func()
        print("C func completed")


c = C()
c.func()
Get MRO list
print(c.__class__.__mro__)
Copy the code

The result is as follows:

In the above program, Base is the parent class, A and B inherit from Base, and C inherit from A and B. Their inheritance relationship is A typical oneDiamond inheritanceThat is as follows:

From the result, we can see that super is not A method to get the parent class and use it to call the parent class, but to call the next class according to the MRO list, using C. __class__.__mro__ to get the MRO list, the order of MRO list is C, A, B, Base, object.

The principle of the super

The super calculation method parses the next class in the order and can take two arguments:

def super(cls, inst) :
    mro = inst.__class__.mro()
    return mro[mro.index(cls) + 1]
Copy the code
  • Responsible for generating MRO list through INST
  • Locate index in MRO list by CLS and return MRO [index + 1]

conclusion

Now we know: supper gets the next class in the MRO list. The parent class of the current class has no substantial relationship. And how to view the MRO list. The last thing to note is the super and MRO lists, which are for new Python classes! Python’s Super () Considered super

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!