The new bytes
type is 3.x only. The 2.x bytes
built-in is just an alias to the str
type. There is no new type called bytes
in 2.x; Just a new alias and literal syntax for str
.
Here’s the documentation snippet everybody loves:
Python 2.6 adds
bytes
as a synonym for
thestr
type, and it also supports the
b''
notation.The 2.6
str
differs from 3.0’s bytes
type in various ways; most notably,
the constructor is completely
different. In 3.0,bytes([65, 66, 67])
is 3 elements long, containing the
bytes representingABC
; in 2.6,
bytes([65, 66, 67])
returns the
12-byte string representing thestr()
of the list.The primary use of
bytes
in 2.6 will
be to write tests of object type such
asisinstance(x, bytes)
. This will
help the2to3
converter, which can’t
tell whether 2.x code intends strings
to contain either characters or 8-bit
bytes; you can now use eitherbytes
or
str
to represent your intention
exactly, and the resulting code will
also be correct in Python 3.0.