What's the rationale for b"..." in this example?

Chris Angelico rosuav at gmail.com
Tue May 15 13:41:21 EDT 2018


On Wed, May 16, 2018 at 1:14 AM, Skip Montanaro
<skip.montanaro at gmail.com> wrote:
> Consider this:
>
>>>> bytes("abc", encoding="utf-8")
> b'abc'
>
> Looks reasonable. Then consider this:
>
>>>> str(bytes("abc", encoding="utf-8"))
> "b'abc'"
>
> Why is the b'...' bit still there? I suppose it's because I didn't tell it
> explicitly how to decode the bytes object, as when I do, I get the expected
> result:
>
>>>> str(bytes("abc", encoding="utf-8"), encoding="utf-8")
> 'abc'
>
> Coming from a still largely Python 2 perspective, did all attempts to apply
> default encodings disappear in Python 3?

It's there for the same reason as the square brackets here:

>>> str(list('abc'))
"['a', 'b', 'c']"

Calling str() on arbitrary objects returns a printable string; and in
Py3, there's simply nothing special about bytes. Personally, I never
use the str(..., encoding="...") notation; I prefer to use the
.decode() method of the bytes object.

ChrisA



More information about the Python-list mailing list