Update docs for buffer -> bytearray rename.
This commit is contained in:
parent
faddf1fb22
commit
9541463a7d
|
@ -118,18 +118,18 @@ available. They are listed here in alphabetical order.
|
||||||
.. index:: pair: Boolean; type
|
.. index:: pair: Boolean; type
|
||||||
|
|
||||||
|
|
||||||
.. function:: buffer([arg[, encoding[, errors]]])
|
.. function:: bytearray([arg[, encoding[, errors]]])
|
||||||
|
|
||||||
Return a new array of bytes. The :class:`buffer` type is an immutable sequence
|
Return a new array of bytes. The :class:`bytearray` type is an immutable
|
||||||
of integers in the range 0 <= x < 256. It has most of the usual methods of
|
sequence of integers in the range 0 <= x < 256. It has most of the usual
|
||||||
mutable sequences, described in :ref:`typesseq-mutable`, as well as most methods
|
methods of mutable sequences, described in :ref:`typesseq-mutable`, as well
|
||||||
that the :class:`str` type has, see :ref:`bytes-methods`.
|
as most methods that the :class:`str` type has, see :ref:`bytes-methods`.
|
||||||
|
|
||||||
The optional *arg* parameter can be used to initialize the array in a few
|
The optional *arg* parameter can be used to initialize the array in a few
|
||||||
different ways:
|
different ways:
|
||||||
|
|
||||||
* If it is a *string*, you must also give the *encoding* (and optionally,
|
* If it is a *string*, you must also give the *encoding* (and optionally,
|
||||||
*errors*) parameters; :func:`buffer` then converts the Unicode string to
|
*errors*) parameters; :func:`bytearray` then converts the Unicode string to
|
||||||
bytes using :meth:`str.encode`.
|
bytes using :meth:`str.encode`.
|
||||||
|
|
||||||
* If it is an *integer*, the array will have that size and will be
|
* If it is an *integer*, the array will have that size and will be
|
||||||
|
@ -148,8 +148,8 @@ available. They are listed here in alphabetical order.
|
||||||
|
|
||||||
Return a new "bytes" object, which is an immutable sequence of integers in
|
Return a new "bytes" object, which is an immutable sequence of integers in
|
||||||
the range ``0 <= x < 256``. :class:`bytes` is an immutable version of
|
the range ``0 <= x < 256``. :class:`bytes` is an immutable version of
|
||||||
:class:`buffer` -- it has the same non-mutating methods and the same indexing
|
:class:`bytearray` -- it has the same non-mutating methods and the same
|
||||||
and slicing behavior.
|
indexing and slicing behavior.
|
||||||
|
|
||||||
Accordingly, constructor arguments are interpreted as for :func:`buffer`.
|
Accordingly, constructor arguments are interpreted as for :func:`buffer`.
|
||||||
|
|
||||||
|
|
|
@ -457,12 +457,12 @@ generator object) supplying the :meth:`__iter__` and :meth:`__next__` methods.
|
||||||
|
|
||||||
.. _typesseq:
|
.. _typesseq:
|
||||||
|
|
||||||
Sequence Types --- :class:`str`, :class:`bytes`, :class:`buffer`, :class:`list`, :class:`tuple`, :class:`range`
|
Sequence Types --- :class:`str`, :class:`bytes`, :class:`bytearray`, :class:`list`, :class:`tuple`, :class:`range`
|
||||||
===============================================================================================================
|
==================================================================================================================
|
||||||
|
|
||||||
There are five sequence types: strings, byte sequences, buffers, lists, tuples,
|
There are five sequence types: strings, byte sequences, byte arrays, lists,
|
||||||
and range objects. (For other containers see the built-in :class:`dict`,
|
tuples, and range objects. (For other containers see the built-in
|
||||||
:class:`list`, :class:`set`, and :class:`tuple` classes, and the
|
:class:`dict`, :class:`list`, :class:`set`, and :class:`tuple` classes, and the
|
||||||
:mod:`collections` module.)
|
:mod:`collections` module.)
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
|
@ -479,24 +479,24 @@ double quotes: ``'xyzzy'``, ``"frobozz"``. See :ref:`strings` for more about
|
||||||
string literals. In addition to the functionality described here, there are
|
string literals. In addition to the functionality described here, there are
|
||||||
also string-specific methods described in the :ref:`string-methods` section.
|
also string-specific methods described in the :ref:`string-methods` section.
|
||||||
|
|
||||||
Bytes and buffer objects contain single bytes -- the former is immutable while
|
Bytes and bytearray objects contain single bytes -- the former is immutable
|
||||||
the latter is a mutable sequence. Bytes objects can be constructed from
|
while the latter is a mutable sequence. Bytes objects can be constructed from
|
||||||
literals too; use a ``b`` prefix with normal string syntax: ``b'xyzzy'``.
|
literals too; use a ``b`` prefix with normal string syntax: ``b'xyzzy'``. To
|
||||||
To construct buffer objects, use the :func:`buffer` function.
|
construct byte arrays, use the :func:`bytearray` function.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
||||||
While string objects are sequences of characters (represented by strings of
|
While string objects are sequences of characters (represented by strings of
|
||||||
length 1), bytes and buffer objects are sequences of *integers* (between 0
|
length 1), bytes and bytearray objects are sequences of *integers* (between 0
|
||||||
and 255), representing the ASCII value of single bytes. That means that for
|
and 255), representing the ASCII value of single bytes. That means that for
|
||||||
a bytes or buffer object *b*, ``b[0]`` will be an integer, while ``b[0:1]``
|
a bytes or bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]``
|
||||||
will be a bytes or buffer object of length 1.
|
will be a bytes or bytearray object of length 1.
|
||||||
|
|
||||||
Also, while in previous Python versions, byte strings and Unicode strings
|
Also, while in previous Python versions, byte strings and Unicode strings
|
||||||
could be exchanged for each other rather freely (barring encoding issues),
|
could be exchanged for each other rather freely (barring encoding issues),
|
||||||
strings and bytes are now completely separate concepts. There's no implicit
|
strings and bytes are now completely separate concepts. There's no implicit
|
||||||
en-/decoding if you pass and object of the wrong type. A string always
|
en-/decoding if you pass and object of the wrong type. A string always
|
||||||
compares unequal to a bytes or buffer object.
|
compares unequal to a bytes or bytearray object.
|
||||||
|
|
||||||
Lists are constructed with square brackets, separating items with commas: ``[a,
|
Lists are constructed with square brackets, separating items with commas: ``[a,
|
||||||
b, c]``. Tuples are constructed by the comma operator (not within square
|
b, c]``. Tuples are constructed by the comma operator (not within square
|
||||||
|
@ -504,10 +504,9 @@ brackets), with or without enclosing parentheses, but an empty tuple must have
|
||||||
the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple
|
the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple
|
||||||
must have a trailing comma, such as ``(d,)``.
|
must have a trailing comma, such as ``(d,)``.
|
||||||
|
|
||||||
Objects of type range are similar to buffers in that there is no specific syntax
|
Objects of type range are created using the :func:`range` function. They don't
|
||||||
to create them, but they are created using the :func:`range` function. They
|
support slicing, concatenation or repetition, and using ``in``, ``not in``,
|
||||||
don't support slicing, concatenation or repetition, and using ``in``, ``not
|
:func:`min` or :func:`max` on them is inefficient.
|
||||||
in``, :func:`min` or :func:`max` on them is inefficient.
|
|
||||||
|
|
||||||
Most sequence types support the following operations. The ``in`` and ``not in``
|
Most sequence types support the following operations. The ``in`` and ``not in``
|
||||||
operations have the same priorities as the comparison operations. The ``+`` and
|
operations have the same priorities as the comparison operations. The ``+`` and
|
||||||
|
@ -1168,16 +1167,16 @@ Mutable Sequence Types
|
||||||
.. index::
|
.. index::
|
||||||
triple: mutable; sequence; types
|
triple: mutable; sequence; types
|
||||||
object: list
|
object: list
|
||||||
object: buffer
|
object: bytearray
|
||||||
|
|
||||||
List and buffer objects support additional operations that allow in-place
|
List and bytearray objects support additional operations that allow in-place
|
||||||
modification of the object. Other mutable sequence types (when added to the
|
modification of the object. Other mutable sequence types (when added to the
|
||||||
language) should also support these operations. Strings and tuples are
|
language) should also support these operations. Strings and tuples are
|
||||||
immutable sequence types: such objects cannot be modified once created. The
|
immutable sequence types: such objects cannot be modified once created. The
|
||||||
following operations are defined on mutable sequence types (where *x* is an
|
following operations are defined on mutable sequence types (where *x* is an
|
||||||
arbitrary object).
|
arbitrary object).
|
||||||
|
|
||||||
Note that while lists allow their items to be of any type, buffer object
|
Note that while lists allow their items to be of any type, bytearray object
|
||||||
"items" are all integers in the range 0 <= x < 256.
|
"items" are all integers in the range 0 <= x < 256.
|
||||||
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
+------------------------------+--------------------------------+---------------------+
|
||||||
|
@ -1271,7 +1270,7 @@ Notes:
|
||||||
sequence.
|
sequence.
|
||||||
|
|
||||||
(7)
|
(7)
|
||||||
:meth:`sort` is not supported by buffer objects.
|
:meth:`sort` is not supported by :class:`bytearray` objects.
|
||||||
|
|
||||||
The :meth:`sort` method takes optional arguments for controlling the
|
The :meth:`sort` method takes optional arguments for controlling the
|
||||||
comparisons.
|
comparisons.
|
||||||
|
@ -1305,13 +1304,13 @@ Notes:
|
||||||
|
|
||||||
.. _bytes-methods:
|
.. _bytes-methods:
|
||||||
|
|
||||||
Bytes and Buffer Methods
|
Bytes and Byte Array Methods
|
||||||
------------------------
|
----------------------------
|
||||||
|
|
||||||
.. index:: pair: bytes; methods
|
.. index:: pair: bytes; methods
|
||||||
pair: buffer; methods
|
pair: bytearray; methods
|
||||||
|
|
||||||
Bytes and buffer objects, being "strings of bytes", have all methods found on
|
Bytes and bytearray objects, being "strings of bytes", have all methods found on
|
||||||
strings, with the exception of :func:`encode`, :func:`format` and
|
strings, with the exception of :func:`encode`, :func:`format` and
|
||||||
:func:`isidentifier`, which do not make sense with these types. For converting
|
:func:`isidentifier`, which do not make sense with these types. For converting
|
||||||
the objects to strings, they have a :func:`decode` method.
|
the objects to strings, they have a :func:`decode` method.
|
||||||
|
@ -1321,7 +1320,7 @@ Wherever one of these methods needs to interpret the bytes as characters
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
The methods on bytes and buffer objects don't accept strings as their
|
The methods on bytes and bytearray objects don't accept strings as their
|
||||||
arguments, just as the methods on strings don't accept bytes as their
|
arguments, just as the methods on strings don't accept bytes as their
|
||||||
arguments. For example, you have to write ::
|
arguments. For example, you have to write ::
|
||||||
|
|
||||||
|
@ -1334,7 +1333,7 @@ Wherever one of these methods needs to interpret the bytes as characters
|
||||||
b = a.replace(b"a", b"f")
|
b = a.replace(b"a", b"f")
|
||||||
|
|
||||||
|
|
||||||
The bytes and buffer types have an additional class method:
|
The bytes and bytearray types have an additional class method:
|
||||||
|
|
||||||
.. method:: bytes.fromhex(string)
|
.. method:: bytes.fromhex(string)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue