22.09.20 16:57, Ethan Furman пише: > On 9/22/20 12:11 AM, Serhiy Storchaka wrote: >> The only exception is StrEnum -- overriding __str__ of str >> subclass may be not safe. Some code will call str() implicitly, other >> will read the string content of the object directly, and they will be >> different. > > Following up on that: > > >>> import enum > >>> > >>> class TestStr(enum.StrEnum): > ... One = '1' > ... Two = '2' > ... Three = '3' > ... > >>> isinstance(TestStr.One, str) > True > > >>> str(TestStr.One) > 'TestStr.One' > > >>> TestStr.One == '1' > True > > I agree, str.__str__ needs to be used in this case.
It is more interesting to compare '%s' % (TestStr.One,) and '{}'.format(TestStr.One). Also str.upper(TestStr.One) and int(TestStr.One) ignore __str__. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@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/Z3UHCVPV6XQ4DP2QZAGNR4IYY565JG3D/ Code of Conduct: http://python.org/psf/codeofconduct/