英文文档
class bytes([source[, encoding[, errors]]])
Return a new “bytes” object, which is animmutable sequence of integers in the range 0 <= x < 256. bytes is animmutable version of bytearray – it has the same non-mutating methods and thesame indexing and slicing behavior.
Accordingly, constructor arguments areinterpreted as for bytearray().
bytes()
bytes([source[, encoding[, errors]]])
1、返回值为一个新的不可修改字节数组,每个数字元素都必须在0 – 255范围内,是bytearray函数的具有相同的行为,差别仅仅是返回的字节数组不可修改。
2、当3个参数都不传的时候,返回长度为0的字节数组
>>> b = bytes()
>>> b
b''
>>> len(b)
0
3、当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组
>>> bytes('中文字符') #需要传入编码格式
Traceback (most recent call last):
File "<pyshell#67&