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

I am new to python and I appreciate the dynamic language. About 30 minutes before my first Python code, I found that the bytes type behaves a little weird (at least):

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

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

Now docs say that strings and bytes behave very similar, except for .format and .encode but I did not expect them to be of the same type. I want to guarantee that I get work with real bytes in my code and that no coercion / encoding / decoding happens.

So what is going on here?

+6
source share
2 answers

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

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

Python 3.0 uses Unicode as the main string type of languages ​​and designates 8-bit literals in different ways, either as b'string' , or using bytes. For future compatibility, Python 2.6 adds bytes as a synonym for str, and also supports b'' notation. b''

2.6 str differs from byte type 3.0s in various ways; most in particular, the constructor is completely different. In 3.0, bytes([65, 66, 67]) - 3 elements long containing bytes representing ABC; in 2.6, bytes([65, 66, 67]) returns a 12-byte string representing str() list.

The main use of bytes in 2.6 will be to write object type tests such as isinstance(x, bytes) . This will help the 2to3 converter, which I can’t say if the 2.x code contains lines containing characters or 8-bit bytes; now you can use either bytes or str to represent your intent exactly, and the resulting code will also fix in Python 3.0.

+10
source

You are looking at Python 3 docs. In Python 2, bytes is an alias for str added to simplify writing code with forwarding support (Python 2 str is a byte string, and in Python 3 str is what was called unicode in Python 2).

See What's New in Python 3.0 for more details.

+8
source

Source: https://habr.com/ru/post/971919/


All Articles