Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is type(bytes()) <'str'>

I'm fairly new to python and I appreciate it's a dynamic language. Some 30 minutes into my first python code, I've discovered that the bytes type behaves a little strange (to say the least):

a = bytes()
print type(a)
// prints: <type 'str'>

Try it here: http://ideone.com/NqbcHk

Now, the docs say strings and bytes behave very similarly with the exception of .format and .encode but I didn't expect they were the same type. What I want to ensure is that I get to work with real bytes in my code, and that no coercion/encoding/decoding occurs.

So what's going on here?

like image 640
Johannes Rudolph Avatar asked Jul 08 '14 13:07

Johannes Rudolph


2 Answers

The bytes type is new in Python 3.x. In Python 2.x, as a compatibility shim, bytes is a simple alias to str.

Read more about this here: https://docs.python.org/2/whatsnew/2.6.html#pep-3112-byte-literals

Python 3.0 adopts Unicode as the language’s fundamental string type and denotes 8-bit literals differently, either as b'string' or using a bytes constructor. For future compatibility, Python 2.6 adds bytes as a synonym for the str 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 representing ABC; in 2.6, bytes([65, 66, 67]) returns the 12-byte string representing the str() of the list.

The primary use of bytes in 2.6 will be to write tests of object type such as isinstance(x, bytes). This will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes; you can now use either bytes or str to represent your intention exactly, and the resulting code will also be correct in Python 3.0.

like image 115
David Heffernan Avatar answered Nov 10 '22 03:11

David Heffernan


You're looking at the Python 3 docs. In Python 2, bytes is an alias for str, added to make it easier to write forward-compatible code (Python 2's str is a byte string, while in Python 3 str is what was called unicode in Python 2).

For more details, see What’s New In Python 3.0.

like image 21
Fred Foo Avatar answered Nov 10 '22 04:11

Fred Foo