Introduction to Python's super () built-in function

Introduction to Python's super () built-in function

Super built-in function official documentation https://docs.python.org/zh-cn/3/library/functions.html#super

Use the super() function to call the method of the parent class in the subclass. For example, use super().__init__() in the __init__() method of the subclass to call the __init__() method of the parent class.

The super() function is actually used to indirectly access the methods of the parent class, not just simply call the parent class method. Use super() to find and call the method of the parent class in the subclass according to the method resolution order (Method Resolution Order, MRO). This method resolution order determines the order in which methods are called in multiple inheritance.

Following is the basic syntax of super() function −

super().method_name(args)

in:

super(): Indicates calling the parent class.

method_name: The name of the parent class method to be called.

args: parameters passed to the parent method.

It should be noted that the super() function can only be used in new-style classes (inherited from object). Python 3.x version only has new-style class definitions. In Python 2.x versions, there are two types of classes: old-style classes and new-style classes. In Python 3.x versions, all classes are new-style classes. It is officially announced that on January 1, 2020, the update of Python 2 will be stopped.

[In Python, the term "new-style class" refers to a class derived from a built-in class named "object".

The main differences between new-style classes and old-style classes are as follows:

Inherit from object (Inheritance from "object"): All new-style classes implicitly or explicitly inherit from the built-in class object, while classic classes do not.

Method Resolution Order (MRO): New-style classes use the C3 algorithm to calculate method resolution order, while classic classes use a depth-first algorithm. MRO determines the order of method lookups, making multiple inheritance more controllable and intuitive.

Feature access control: New-style classes provide @property decorator and property() function, making property access more flexible and controllable.

Constructor: New-style classes introduce the __new__() method and the super() function, making instance creation and initialization more customizable and flexible.

The following is an example of using the super built-in function:

class Parent:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print(f"Hello, {self.name}!")

#子类Child继承自父类Parent
class Child(Parent):
    def __init__(self, name, age):
        super().__init__(name) #使用super().__init__()调用父类Parent的构造函数
        self.age = age

    def greet(self):
        super().greet() #调用父类的greet()方法
        print(f"I'm {self.age} years old.")

child = Child("Alice", 10) #创建了一个Child类的实例child,并传入名字"Alice"和年龄10。
child.greet()

The output is as follows:

Hello, Alice!
I'm 10 years old.

In the above example, the Child class inherits from the Parent class. By using the super() function, we call the __init__ method of the parent class in the __init__ method of the Child class and pass the necessary parameters.

In the Child class, we specify the inheritance relationship through Child (Parent), and use Parent as the base class. The Child class defines a new constructor __init__(self, name, age) that takes a name and an age as parameters. By calling super().__init__(name), we call the constructor of the parent class in the child class so that the initialization logic in the parent class is executed. We then store the passed in age in the instance variable self.age. In addition, the Child class also overrides the greet() method of the parent class. In the greet() method of the subclass, we first use super().greet() to call the greet() method of the parent class to output the greeting of the parent class: "Hello, Alice!". Then, age-specific information for the subclass: "I'm 10 years old.".

Detailed explanation of super() in Python and examples of application scenarios https://zhuanlan.zhihu.com/p/415729655

Detailed explanation of Python MRO method analysis sequence https://www.zhihu.com/tardis/zm/art/416584599

Guess you like

Origin blog.csdn.net/cnds123/article/details/131791169