Python program structure [2], class/class, built-in class bytes and ByteArray

Source: Internet
Author: User

built-in classes bytes and bytearray/built-in type bytes and ByteArray

About built-in classes

Python's built-in class bytes has the following main points:

Class bytes ([source[, encoding[, errors]])

Return a new "bytes" object, which is an immutable sequence of integers in the range 0 <= x < 256. Bytes is a immutable version of Bytearray–it has the same non-mutating methods and the same indexing and slicing Behav Ior.

Accordingly, constructor arguments is interpreted as for ByteArray ().

1. The return value is a new non-modifiable byte array, each number element must be within the range of 0-255, is the ByteArray function has the same behavior, the difference is only the returned byte array is not modifiable ;

2. Returns a byte array with a length of 0 when none of the 3 parameters are transmitted;

3. The encoding parameter must also be provided when the source parameter is a string, and the function converts the string to a byte array using the Str.encode method;

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

5. When the source parameter is a type object that implements the buffer interface, the read-only method is used to return the bytes to the byte array;

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;

7. The returned array cannot be modified.

>>> bytes (1)  b'\x00'  >>> bytes ([1, 2, 3])  b'\x01\x02\x03'  

The biggest difference between bytes and ByteArray is that bytes-generated objects are not modifiable.

1s ='@MOVE \ r \ n'2 3m = ByteArray (S, encoding='ASCII')4x = bytes (S, encoding='ASCII')5 6M[0] = 657 Try:8X[0] = 659 except:Ten     Pass One  A Print(m, X, sep='\ n')

From the output you can see that M is modified and X has not been modified

ByteArray (b'amove\r\n')  b'@MOVE \ r \ n'  

Related reading

1. About the built-in class

Reference Links

Http://www.cnblogs.com/sesshoumaru/p/5980090.html

Python program structure [2], class/class, built-in class bytes and ByteArray

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.