SoFunction
Updated on 2024-10-30

How to use super in python

Technical background

super in python, named super class, can be simply understood as executing the __init__ function of the parent class. Because in python, whether it is one-to-one inheritance, or a subclass inherits more than one parent class, it will involve the execution of the order of the problem. So this article will focus on the specific role of super.

Case Test

By designing such a case, we can clarify the logical relationship before and after super: first define a parent classinitial, there are parameter values in this parent classparamsum function (math.)funcand then subclass it withnewto inherit from the parent classinitial. After inheritance, a subclass of__init__in a functionsuperGo to print parameter values before and after executionparamsum function (math.)funcThe related code is shown below:

# Define the parent class
class initial(object):
    def __init__(self):
        print ('This print is from initial object')
        # Define parent class parameters
         = 3

    # Define parent class functions
    def func(self):
        return 1
# Define subclasses
class new(initial):
        print ('This print is from new object')
        # Print subclass function values
        print (())
        # Execute the parent class initialization function
        super(new, self).__init__()
        # Print parent class parameter values
        print()
         = 4
    # Define subclass functions
        return 2
if __name__ == '__main__':
    new()

The result of the code execution is shown below:

This print is from new object
2
This print is from initial object
3

Analysis of results

First we note that the parent classinitialhit the nail on the head__init__The print statement within the function, which is output after super, shows that thesuperfunction is performing the initialization operation of the parent class. Then if the initialization operation is not performed by thesupernewsubclass toinitialWhere does inheritance from the parent class manifest itself? The answer is the member functions of the parent class, as in a case like this:

class initial(object):
    def __init__(self):
        print ('This print is from initial object')
         = 3
    def func(self):
        return 1
class new(initial):
    def __init__(self):
        print ('This print is from new object')
        print (())
        super(new, self).__init__()
        print()
         = 4

if __name__ == '__main__':
    new()

It actually deletes the overloaded member function in the subclass, then the result obtained is as follows:

This print is from new object
1
This print is from initial object
3

You can find that you can print the parent class'sfuncfunction's function value. So the logic of inheritance in python is this:

It is also because only the implementation ofsuperto initialize a member variable in the parent class, so you can't access a member variable in the parent class if it's before super.

Summary outline

In this article, through the design of a real case of python, to explain the object-oriented techniques of python - the logic of the super function that must be used in the inheritance of the class. In fact, we can understand the inheritance of classes in python as a process like this: when we specify the parent class in parentheses, we have actually referenced the member functions of the parent class, but did not execute the initialization function of the parent class. While executing the initialization function of the subclass, it will check whether the parent's member function is overloaded, and if it is overloaded, it will be directly overwritten. Only after the execution of super, it is equivalent to the execution of the initialization function of the parent class, and then you can access the member variables of the parent class.

copyright statement

This article was first linked to:/dechinphy/p/

Author ID: DechinPhy

For more original articles, see:/dechinphy/

Rewards-only link:/dechinphy/gallery/image/

Tencent Cloud Column Synchronization:/developer/column/91958

To this point this article on python in the super is what? The article is introduced to this, more related to python in the super content please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!