In the post ,super() was described as: describe super()
super() looks at the next class in the MRO (method resolution order, accessed with cls.__mro__) to call the methods.
Some material even give a more clear definition as: more clear definition on super()
def super ( cls , inst ) :
mro = inst.__class__.mro( )
return mro[mro.index( cls ) + 1 ]
Create a multiple inheritnce class struture as below:
class state():
def __init__(self):
pass
class event():
def __init__(self):
pass
class happystate(state,event):
def __init__(self):
print(super())
print(super(happystate,self))
The mro list :
>>> happystate.__mro__
(<class '__main__.happystate'>, <class '__main__.state'>, <class '__main__.event'>, <class 'object'>)
The super()
in happystate
class will look at the next class in the MRO, it is state
class in this status.
x=happystate()
<super: <class 'happystate'>, <happystate object>>
<super: <class 'happystate'>, <happystate object>>
Why super()
in happystate
class point to itself instead of the next class in MRO list----state
?
If super()
point to state
,the output should be as:
x=happystate()
<super: <class 'state'>, <state object>>
<super: <class 'state'>, <state object>>