Python implements methods that subclasses call their superclasses

  • 2020-04-02 14:19:04
  • OfStack

This example shows how a Python implementation subclass can call a superclass. Share with you for your reference. The specific implementation method is as follows:

Python is similar to other object-oriented languages in that each class can have one or more parent classes that inherit properties and methods from the parent class. If a method is called in an instance of a subclass, or a property is accessed in an instance of a subclass, but the method or property does not exist in the subclass, it is automatically looked up in its parent class.

After inheriting the parent class, you can call the parent class methods and access the parent class properties, and to complete the integration process, the subclass needs to call the constructor.

Problems arise when subclasses do not explicitly call the parent constructor, and the parent constructor initializes some properties
If both the subclass and the parent class have constructors, the subclass overrides the constructor of the parent class. If the parent class constructor is not explicitly called, the constructor of the parent class will not be executed, causing problems for the subclass instance to access the initial variable in the parent class initialization method.

Let's take a look at the following example:

class A:
    def __init__(self):
        self.namea="aaa"
    def funca(self):
        print "function a : %s"%self.namea
class B(A):
    def __init__(self):
        self.nameb="bbb"
    def funcb(self):
        print "function b : %s"%self.nameb
b=B()
print b.nameb
b.funcb()
b.funca()

Operation results:
bbb
function b : bbb
Traceback (most recent call last):
  File "D:workbenchpythonMyPythonProjectteststudyoverwrite_method.py", line 19, in <module>
    print b.funca()
  File "D:workbenchpythonMyPythonProjectteststudyoverwrite_method.py", line 6, in funca
    print "function a : %s"%self.namea
AttributeError: B instance has no attribute 'namea'

In a subclass, the constructor is overridden, but the new constructor does not have any code to initialize the parent class's namea property, and to achieve the desired effect, the subclass's constructor must call the parent class's constructor for basic initialization. There are two ways to do this: call the unbound version of the superclass constructor, or use the super function.

Method 1: invokes the unbound superclass constructor

Modify the code by adding one more line:

class A:
    def __init__(self):
        self.namea="aaa"
    def funca(self):
        print "function a : %s"%self.namea
class B(A):
    def __init__(self):
        # This row solves the problem
        A.__init__(self)
        self.nameb="bbb"
    def funcb(self):
        print "function b : %s"%self.nameb
b=B()
print b.nameb
b.funcb()
b.funca()

The commented line above solves this problem by simply calling its constructor with the parent class name.

This method is called calling the parent class's unbound constructor. When an instance's method is called, the method's self parameter is automatically bound to the instance (called the bound method). However, if you call the methods of the class directly (such as a. arbitration init), then no instance will be bound. This gives you the freedom to provide the required self parameters, which is called the unbound method.
By providing the current instance as a self parameter to the unbound method, class B can use all the implementations of its superclass constructor and the namea variable is set.

Method 2: use the super function

Modify the code, this time we need to add 2 lines on the original code:

# The parent class needs inheritance object object 
class A(object):
    def __init__(self):
        self.namea="aaa"
    def funca(self):
        print "function a : %s"%self.namea
class B(A):
    def __init__(self):
        # This line solves the problem
        super(B,self).__init__()
        self.nameb="bbb"
    def funcb(self):
        print "function b : %s"%self.nameb
b=B()
print b.nameb
b.funcb()
b.funca()

The first sentence in the annotated new code above lets class A inherit from the object class to use the super function, because this is A feature supported by python's "new classes." The current ray and object can be used as arguments to the super function, and any method that calls the object returned by the function is a method that calls the superclass, not the method of the current class.

The super function returns a super object that does the method parsing, which automatically looks up all the parent classes and their parent classes.

Method 1 is more intuitive, and method 2 initializes all the superclasses at once

The super function is more intuitive than calling an unbound method directly in overwork, but its biggest advantage is that if a subclass inherits from more than one parent class, it only needs to use the super function once. However, if this requirement is not available, it is more intuitive to use A.

I hope this article has helped you with your Python programming.


Related articles: