Chris Angelico wrote:
ANY object can be passed to str() in order to get some sort of valid printable form. The awkwardness comes from the fact that str() performs double duty - it's both "give me a printable form of this object" and "decode these bytes into text".
While it does make sense for str() to be able to give some form of
printable form for any object, I suppose that I just don't consider something
like this: "b'\\xc3\\xa1'" to be overly useful, at least for any practical
purposes. Can anyone think of a situation where you would want a string
representation of a bytes object instead of decoding it?
If not, I think it would be more useful for it to either:
1) Raise a TypeError, assume that the user wanted to decode the string but
forgot to specify an encoding
2) Implicitly decode the bytes object as UTF-8, assume the user meant
str(bytes_obj, encoding='utf-8')
Personally, I'm more in favor of (1) since it's much more explicit and
obvious, but I think (2) would at least be more useful than the current
behavior.
On Sun, Dec 15, 2019 at 8:13 PM Chris Angelico
On a related note though, I'm not a fan of this behavior:
str(b'\xc3\xa1') "b'\\xc3\\xa1'"
Passing a bytes object to str() without specifying an encoding seems
bytes('á') ... TypeError: string argument without an encoding
I'd much prefer to see something like this:
str(b'\xc3\xa1') ... TypeError: bytes argument without an encoding
Is there some use case for returning "b'\\xc3\\xa1'" from this operation
On Mon, Dec 16, 2019 at 12:00 PM Kyle Stanley
wrote: like a mistake, I honestly don't see how this ("b'\\xc3\\xa1'") would even be useful in any capacity. I would expect this to instead raise a TypeError, similar to passing a string to bytes() without specifying an encoding: that I'm not seeing? To me, it seems equally, if not more confusing and pointless than returning an empty string from str(errors='strict') or some other combination of *errors* and *encoding* kwargs without passing an object. ANY object can be passed to str() in order to get some sort of valid printable form. The awkwardness comes from the fact that str() performs double duty - it's both "give me a printable form of this object" and "decode these bytes into text". With an actual bytes object, I always prefer b.decode(...) to str(b, encoding=...). But the one-arg form of str() needs to be able to represent a bytes object in some way, just as it can represent an int, a Fraction, or a list.
ChrisA _______________________________________________ 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/ZP7SXIDQ... Code of Conduct: http://python.org/psf/codeofconduct/