Python built-in functions (7) -- bytearray, pythonbytearray

Source: Internet
Author: User

Python built-in functions (7) -- bytearray, pythonbytearray

English document:

Classbytearray([Source[,Encoding[,Errors])

Return a new array of bytes.bytearrayClass is a mutable sequence of integers in the range 0 <= x <256. it has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods thatbytesType has, see Bytes and Bytearray Operations.

The optionalSourceParameter can be used to initialize the array in a few different ways:

  • If it isString, You must also giveEncoding(And optionally,Errors) Parameters;bytearray()Then converts the string to bytes usingstr.encode().
  • If it isInteger, The array will have that size and will be initialized with null bytes.
  • If it is an object conforming toBufferInterface, a read-only buffer of the object will be used to initialize the bytes array.
  • If it isIterable, It must be an iterable of integers in the range0 <= x < 256, Which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

Note:

1. The returned value is a new byte array.

2. If none of the three parameters are passed, a byte array with a length of 0 is returned.

>>> b = bytearray()>>> bbytearray(b'')>>> len(b)0

 

3. When the source parameter is a string, the encoding parameter must also be provided. The function converts the string to a byte array using the str. encode method.

>>> Bytearray ('Chinese') Traceback (most recent call last): File "<pyshell #48>", line 1, in <module> bytearray ('Chinese ') typeError: string argument without an encoding >>>> bytearray ('Chinese', 'utf-8') bytearray (B '\ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 ')

 

4. When the source parameter is an integer, an empty byte array of the length specified by this integer is returned.

>>> Bytearray (2) bytearray (B '\ x00 \ x00') >>> bytearray (-2) # The integer must be greater than 0, traceback (most recent call last): File "<pyshell #51>", line 1, in <module> bytearray (-2) ValueError: negative count

 

5. When the source parameter is an object that implements the buffer Interface, read-only bytes to the byte array and return

6. When the source parameter is an iteratable object, all elements of this iteration object must conform to 0 <= x <256, so that they can be initialized to the array.

>>> Bytearray ([, 3]) bytearray (B '\ x01 \ x02 \ x03') >>> bytearray ([, 3]) # error Traceback (most recent call last): File "<pyshell #53>", line 1, in <module> bytearray ([, 2, 3]) ValueError: byte must be in range (0,256)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.