Summary: python implements the difference between the two methods called by the parent class, and python implements the two methods.
There are two methods in python that can call the method of the parent class:
super(Child, self).method(args)
Parent.method(self, args)
I reported the following error using one of the methods:
Classobj cannot be found. When I change the callsuper(B, self).f(name)
And the results are correct.
Analysis Error
Because the base class does not inherit objects, in python, one can be created as follows:
class A: pass
You can also create it as follows:
class A(object): pass
The difference between the two is:
This is the difference between the old class (the former) and the new class (the latter. Differences can be referred to: https://docs.python.org/release/2.5.2/ref/node33.html
Python3 has removed the old type, that is, it has implicitly inherited the object. Therefore, there is no difference between writing and inheriting objects in python3.
Differences between the two calls
Parent.__init__(self)
Andsuper(Child, self).__init__()
What is the difference?
Super is taken for granted as a parent class. In python, it actually refers to the next class in MRO!
Super actually did this. Let's look at the answer:
def super(cls, inst): mro = inst.__class__.mro() # Always the most derived class return mro[mro.index(cls) + 1]
MRO stands for Method Resolution Order, which represents the sequence of class inheritance.
Super is used to solve the problem of multiple inheritance. Assume that B C D directly inherits class
class E(B, C, D): def __init__(self): # code...
If the class E constructor usessuper(E, self).__init__()
The constructor of Class A is executed once, And the constructor of Class A is executed multiple times in another method.
In MRO, the base class always appears after the derived class. If there are multiple base classes, the relative sequence of the base classes remains unchanged.
Summary
I personally think that a complex inheritance structure is a poor design in design. When the inheritance structure is clear, there is no difference between the two methods. The above is all the content of this article. I hope this article will help you in your study or work.