multiple inheritance and `__str__`
I ran into an issue today with `str()` not behaving as I thought it should. Given the following test script, what should happen? -- 8< ------------------------------ class Blah(object): def __str__(self): return 'blah' class Huh(int, Blah): pass class Hah(Blah, int): pass huh = Huh(7) hah = Hah(13) print(huh) print(hah) -- 8< ------------------------------ I thought I would get: 7 blah and indeed, that is what I got for Pythons 2.7 - 3.7. However, starting with Python 3.8 I started getting: blah blah To say the least, I was surprised. Some searching turned up issue 36793: "Do not define unneeded __str__ equal to __repr__" . As can be seen, `__str__` is needed for inheritance to work properly. Thoughts on reverting that patch? -- ~Ethan~
01.12.21 09:56, Ethan Furman пише:
Some searching turned up issue 36793: "Do not define unneeded __str__ equal to __repr__" .
As can be seen, `__str__` is needed for inheritance to work properly. Thoughts on reverting that patch?
As the author of issue36793 I do not think so. If you replace int with list you will get the same result. If you make Blah an int subclass you will get the same result. If you want to get different results set Huh.__str__ explicitly to object.__str__ or int.__repr__.
participants (2)
-
Ethan Furman -
Serhiy Storchaka