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'm not saying it's the best practice, but calling str() on an object is a currently a guaranteed way of making a string out of it, and I don't think we can change it. Eric