Mailing List Archive

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~
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/GLHE6CJ3IEHPUTA36YWOF5FSDLYNIOCV/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: multiple inheritance and `__str__` [ In reply to ]
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__.

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/U3VVGYXXQ4RED64SLCX63BJITCCVLJFL/
Code of Conduct: http://python.org/psf/codeofconduct/