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 Versio N 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 ('English')#need to pass in encoding formatTraceback (most recent): File"<pyshell#14>", Line 1,inch<module>bytes ('English') typeerror:string argument without an encoding>>> bytes ('English','Utf-8') b'\xe4\xb8\xad\xe6\x96\x87'>>>'English'. 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'# integers need to be greater than 0 for array lengths Traceback (most recent): '<pyshell#19>' 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 (recent): "<pyshell#21>" in <module > bytes ([256,2,3 in range (0, 256)
7. Return array not modifiable
>>> B = Bytes (10)>>>BB'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>>>b[0]0>>> B[1] = 1#cannot be modifiedTraceback (most recent): File"<pyshell#6>", Line 1,inch<module>b[1] = 1TypeError:'bytes'Object does notSupport Item Assignment>>> B = ByteArray (10)>>>Bbytearray (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')>>> B[1] = 1#can be modified>>>Bbytearray (b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00')
Python built-in functions (8)--bytes