bool, complex, float, intbytearray, bytes, strdict, frozenset, list, set, tuplememoryview, object, property, range, slice, type
Draft for Information Only
Content
Python Built-in Class Functions bytearray() Parameters Remarks bytes() Parameters Remarks str() Parameters Remarks Source and Reference
Python Built-in Class Functions
The Python interpreter has some built-in class functions.
bytearray()
class bytearray([source[, encoding[, errors]]])
Parameters
bytearray()to return a new array of bytes
[source]to specify the source to return from
[encoding]to specify the encoding to be used for
[errors]to specify the errors handling to be used for
Remarks
- Return a new array of bytes. The bytearray class 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 that the bytes type has, see Bytes and Bytearray Operations.
The optional source parameter can be used to initialize the array in a few different ways:
If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
bytes()
class bytes([source[, encoding[, errors]]])
Parameters
bytes()to return a new array of bytes
[source]to specify the source to return from
[encoding]to specify the encoding to be used for
[errors]to specify the errors handling to be used for
Remarks
- Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.
Accordingly, constructor arguments are interpreted as for bytearray().
Bytes objects can also be created with literals, see String and Bytes literals.
str()
class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
Parameters
str()to return a string version of object
object=''to specify the object to be reurned from
object=b''to specify the object to be reurned from
encoding='utf-8'to specify the encoding to be used for
errors='strict'to specify the errors handling to be used for.
Remarks
- Return a string version of object. If object is not provided, returns the empty string. Otherwise, the behavior of str() depends on whether encoding or errors is given, as follows.
If neither encoding nor errors is given, str(object) returns object.__str__(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a __str__() method, then str() falls back to returning repr(object).
If at least one of encoding or errors is given, object should be a bytes-like object (e.g. bytes or bytearray). In this case, if object is a bytes (or bytearray) object, then str(bytes, encoding, errors) is equivalent to bytes.decode(encoding, errors). Otherwise, the bytes object underlying the buffer object is obtained before calling bytes.decode(). See Binary Sequence Types — bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects.
Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the -b command-line option to Python).
Source and Reference
©sideway
ID: 200700302 Last Updated: 7/3/2020 Revision: 0
|
|