On Mon, Dec 16, 2019 at 12:00 PM Kyle Stanley <aeros...@gmail.com> wrote: > 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 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: > >>> 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 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-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/ZP7SXIDQOQVKUF66NVZPS3O4FN3A6DWA/ Code of Conduct: http://python.org/psf/codeofconduct/