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.