|
|
Subscribe / Log in / New account

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)

With Python 3.10.7, the following command didn't print "Top" for the last line as expected and per the article. Who else got a similar result?

>>> 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'>]
>>>


to post comments

Bottom2().method() didn't print Top

Posted Oct 16, 2022 1:01 UTC (Sun) by thomastthai (guest, #161592) [Link]

I realized 'super(Right, self).method' was missing '()'. Added them and the output is now correct.

class Bottom2(Left, Right):
def method(self):
print('Bottom2')
super(Right, self).method # <==========

>>> class Bottom2(Left, Right):
... def method(self):
... print('Bottom2')
... super(Right, self).method()
...
>>> Bottom2().method()
Bottom2
Top
>>>


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds