English documents:
class bytes ([source[, encoding[, errors]])
Return a new "bytes" object, which is an immutable sequence of integers in the range 0 <= x < 256 . was an bytes immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.
Accordingly, constructor arguments is interpreted as for bytearray() .
Description
1. The return value is a new non-modifiable byte array, each number element must be within the range of 0-255, the ByteArray function has the same behavior, the difference is simply that the returned byte array cannot be modified.
2. Returns a byte array with a length of 0 when none of the 3 parameters are passed
>>> b = bytes () >>> BB ">>> len (b) 0
3. The encoding parameter must also be supplied when the source parameter is a string, and the function converts the string to a byte array using the Str.encode method
>>> bytes (' Chinese ') #需传入编码格式Traceback (most recent): File "<pyshell#14>", line 1, in <module& gt; Bytes (' Chinese ') typeerror:string argument without an encoding>>> bytes (' Chinese ', ' utf-8 ') B ' \xe4\xb8\xad\xe6\x96\x87 ' >>> ' Chinese '. Encode (' utf-8 ') B ' \xe4\xb8\xad\xe6\x96\x87 '
4. Returns an empty byte array of the length specified by this integer when the source parameter is an integer
>>> bytes (2) b ' \x00\x00 ' >>> bytes ( -2) #整数需大于0 for array length traceback (most recent call last): File " <pyshell#19> ", line 1, in <module> bytes ( -2) valueerror:negative count
5. When the source parameter is a type object that implements the buffer interface, a read-only method is used to read the bytes to the byte array and return
6. When the source parameter is an iterative object, the elements of the iteration object must conform to 0 <= x < 256 so that it can be initialized into the array
>>> bytes ([i]) b ' \x01\x02\x03 ' >>> bytes ([256,2,3]) Traceback (most recent call last): File " <pyshell#21> ", line 1, in <module> bytes ([256,2,3]) Valueerror:bytes must is in range (0, 256)
7. Return array not modifiable
>>> B = Bytes (ten) >>> BB ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ' >>> b[0]0>>> b[ 1] = 1 #不可修改Traceback (most recent): File ' <pyshell#6> ', line 1, in <module> b[1] = 1TYPEERR Or: ' bytes ' object does not support item assignment>>> B = ByteArray (Ten) >>> Bbytearray (b ' \x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00 ') >>> b[1] = 1 #可修改 >>> bbytearray (b ' \x00\x01\x00\x00\x00\x00\x00\x00\ X00\x00 ')
Python built-in function--bytes