In Python 2.6, bytes is str

B

Bryan Olson

Python 3 has the 'bytes' type, which the string type I've long wanted in
various languages. Among other advantages, it is immutable, and
therefore bytes objects can be dict keys. There's a mutable version too,
called "bytearray".

In Python 2.6, the name 'bytes' is defined, and bound to str. The 2.6
assignment presents some pitfalls. Be aware.

Consider constructing a bytes object as:

b = bytes([68, 255, 0])

In Python 3.x, len(b) will be 3, which feels right.

In Python 2.6, len(b) will be 12, because b is the str, '[68, 255, 0]'.


I'm thinking I should just avoid using 'bytes' in Python 2.6. If there's
another Python release between 2.6 and 3.gold, I'd advocate removing
the pre-defined 'bytes', or maybe defining it as something else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top