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

python, python-2.7

Solution

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.

Problem

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?

Original source