Python super () calls the parent class method, but the self in the parent class is not an instance of the parent class but an instance of the subclass?

I found such a problem in the process of learning Python . I looked up some materials but still did not understand it, as follows:

version is python3.6

the code is as follows:

print("mro", Y.mro()) -sharp mro [<class "__main__.Y">, <class "__main__.X">, <class "object">]

so according to the MRO list, the following class is X , then super (). F () is actually X.f () , then the return self.x should be the X instance, but the actual running results show that self is an instance of the subclass Y . Since super (). F () here calls the method of the parent class, shouldn"t the self refer to the parent class instance? This is difficult for me to understand.

the expression is a little confused. I hope there are seniors who can give me an answer. I would appreciate it!

question add:

one more thing came to mind when I wrote this question:

when inheriting, there is often super (). _ init__ () to call the _ init__ () function of the parent class. When calling this function, the self actually refers to the subclass instance? It seems to have the same meaning as the previous question, but I don"t know how to understand it. I hope I can have some advice from my seniors.

Oct.25,2021

  1. execute the sentence yearly () to generate only one instance, that is, an instance of a subclass
  2. because the subclass Y has its own initialization method, and the initialization method of the calling parent class is not displayed, the initialization method of X will not be called.
  3. Y is a subclass of X . Y inherits the f () method of X . Calling super (). F () does not execute the initialization function of X . super (). F () is an abbreviated form, which should be written as super (Yjournal self). F () ). The execution process is as follows

    .
    • find the parent class of Y (that is, the class X )
    • convert the object y of class Y to the object of its parent class X , and call the f () method
    • on the surface, the calling body is the object of the class X , but it is actually the object y .
  4. x and y are two different objects, and they don't have a dime relationship.

add some log, to the above code and then take a look at it; communicate again if you have any questions ~

class A(object):

    def a(self):
        pass
    def b(self):
        pass

class B(A):

    def b(self):
        pass

    def __init__(self):
        print super(B,self).a == self.a //True
        print super(B,self).b == self.b //False


def main():
    B()


if __name__ == '__main__':
    main()

We see that when the subclass does not override the method of the parent class, the method of the subclass is equal to the method of the parent class, and the address in memory is all the same.
so in your example super (). F () is no different from the direct f () .
the same. In the instance of the subclass, an instance of the parent class will not inexplicably pop up as the self parameter to the f () method. Moreover, the init method of your subclass overrides the init method of the parent class, so the init method of the parent class is not executed at all. From that point of view, it will never be equal to the value initialized by the parent class.

Menu