Bottom2().method() didn't print Top
Bottom2().method() didn't print Top
Posted Oct 15, 2022 23:09 UTC (Sat) by thomastthai (guest, #161592)Parent article: Super Python (part 2)
>>> Bottom2().method()
Bottom2
-----------------------------------------------
>>> import platform
>>> platform.python_version()
'3.10.7'
>>> class Top:
... def method(self):
... print('Top')
...
>>>
>>> class Left(Top):
... def method(self):
... print('Left')
... super().method()
...
>>> class Right(Top):
... def method(self):
... print('Right')
... super().method()
...
>>> class Bottom(Left, Right):
... def method(self):
... print('Bottom')
... super(Left, self).method()
...
>>> class Bottom2(Left, Right):
... def method(self):
... print('Bottom2')
... super(Right, self).method
...
>>> Bottom().method()
Bottom
Right
Top
>>>
>>> Bottom.mro()
[<class '__main__.Bottom'>, <class '__main__.Left'>, <class '__main__.Right'>, <class '__main__.Top'>, <class 'object'>]
>>> Bottom2().method()
Bottom2
>>>
>>> Bottom2.mro()
[<class '__main__.Bottom2'>, <class '__main__.Left'>, <class '__main__.Right'>, <class '__main__.Top'>, <class 'object'>]
>>>
Posted Oct 16, 2022 1:01 UTC (Sun)
by thomastthai (guest, #161592)
[Link]
class Bottom2(Left, Right):
>>> class Bottom2(Left, Right):
Bottom2().method() didn't print Top
def method(self):
print('Bottom2')
super(Right, self).method # <==========
... def method(self):
... print('Bottom2')
... super(Right, self).method()
...
>>> Bottom2().method()
Bottom2
Top
>>>