Are Python byte objects also known as strings?

This is the section from Dive Into Python 3 regarding strings:

In Python 3, all strings are Unicode character sequences. There is no such thing as a Python string encoded in utf-8 or a Python string encoded as CP-1252. "Is this a utf-8 string?" is an invalid question. utf-8 is a way to encode characters as a sequence of bytes. If you want to take a string and turn it into a sequence of bytes in a specific character encoding, Python 3 will help you with this. If you want to take a sequence of bytes and turn it into a string, Python 3 will also help you with this. Bytes are not characters; bytes are bytes. Symbols are an abstraction. A string is a sequence of these abstractions.

Earlier today I used the module hashliband read the help text for md5, which reads:

Returns a new hash file MD5; possibly initialized with a string.

Well, he does not accept string- he accepts the object bytes.

Maybe I read too much about this, but it doesn't make sense if you should use the help text instead bytes? Or do people use the same name for strings and bytes?

+5
source share
2 answers

In Python 2 and 3, it was strused for both character strings and bytes. In fact, before Python 2.6 there was not even a type bytes(both in 2.6 and 2.7, bytes is str).

The indicated inconsistencies in the hashlib documentation are an artifact of this story.

+6
source

Probably the help remains from Python2.

This is one of the big changes from 2 to 3

    Python2 Python3

    str bytes
    unicode          str

Python2.6 + , bytes str

( - 3.1.2). , , , .

+5

All Articles