16.12.19 10:34, Eric V. Smith пише:
On 12/16/2019 3:05 AM, Kyle Stanley wrote:
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?

Debugging. I sometimes do things like: print('\n'.join(str(thing) for thing in lst)), or various variations on this. This is especially useful when maybe something in the list is a bytes object where I was expecting a string.

I usually create a list:

    print([a, b, c])

It guarantees that repr() be used instead of str(). It also makes the debug output more distinguishable from normal output.

I use %r or !r when include an arbitrary object in logging or error messages. It is safer for several reasons.

But I agree that making str() failing for bytes can break a lot of existing code.
_______________________________________________
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/TNIMOXKSV47XWXXDRBHU2NCKNPPXIZYI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to