mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] GH-138465: Improve documentation for common sequence methods (GH-138474) (#138560)
GH-138465: Improve documentation for common sequence methods (GH-138474)
(cherry picked from commit 8ed1d53e62)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
cddb7e6a7a
commit
f153e32cba
20 changed files with 187 additions and 125 deletions
|
|
@ -245,7 +245,6 @@
|
||||||
('py:attr', '__annotations__'),
|
('py:attr', '__annotations__'),
|
||||||
('py:meth', '__missing__'),
|
('py:meth', '__missing__'),
|
||||||
('py:attr', '__wrapped__'),
|
('py:attr', '__wrapped__'),
|
||||||
('py:meth', 'index'), # list.index, tuple.index, etc.
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# gh-106948: Copy standard C types declared in the "c:type" domain and C
|
# gh-106948: Copy standard C types declared in the "c:type" domain and C
|
||||||
|
|
|
||||||
|
|
@ -589,9 +589,9 @@ exhaustive test suites that exercise every line of code in a module.
|
||||||
An appropriate testing discipline can help build large complex applications in
|
An appropriate testing discipline can help build large complex applications in
|
||||||
Python as well as having interface specifications would. In fact, it can be
|
Python as well as having interface specifications would. In fact, it can be
|
||||||
better because an interface specification cannot test certain properties of a
|
better because an interface specification cannot test certain properties of a
|
||||||
program. For example, the :meth:`!list.append` method is expected to add new elements
|
program. For example, the :meth:`list.append` method is expected to add new elements
|
||||||
to the end of some internal list; an interface specification cannot test that
|
to the end of some internal list; an interface specification cannot test that
|
||||||
your :meth:`!list.append` implementation will actually do this correctly, but it's
|
your :meth:`list.append` implementation will actually do this correctly, but it's
|
||||||
trivial to check this property in a test suite.
|
trivial to check this property in a test suite.
|
||||||
|
|
||||||
Writing test suites is very helpful, and you might want to design your code to
|
Writing test suites is very helpful, and you might want to design your code to
|
||||||
|
|
|
||||||
|
|
@ -454,7 +454,7 @@ There are two factors that produce this result:
|
||||||
(the list), and both ``x`` and ``y`` refer to it.
|
(the list), and both ``x`` and ``y`` refer to it.
|
||||||
2) Lists are :term:`mutable`, which means that you can change their content.
|
2) Lists are :term:`mutable`, which means that you can change their content.
|
||||||
|
|
||||||
After the call to :meth:`!append`, the content of the mutable object has
|
After the call to :meth:`~sequence.append`, the content of the mutable object has
|
||||||
changed from ``[]`` to ``[10]``. Since both the variables refer to the same
|
changed from ``[]`` to ``[10]``. Since both the variables refer to the same
|
||||||
object, using either name accesses the modified value ``[10]``.
|
object, using either name accesses the modified value ``[10]``.
|
||||||
|
|
||||||
|
|
@ -1397,9 +1397,9 @@ To see why this happens, you need to know that (a) if an object implements an
|
||||||
:meth:`~object.__iadd__` magic method, it gets called when the ``+=`` augmented
|
:meth:`~object.__iadd__` magic method, it gets called when the ``+=`` augmented
|
||||||
assignment
|
assignment
|
||||||
is executed, and its return value is what gets used in the assignment statement;
|
is executed, and its return value is what gets used in the assignment statement;
|
||||||
and (b) for lists, :meth:`!__iadd__` is equivalent to calling :meth:`!extend` on the list
|
and (b) for lists, :meth:`!__iadd__` is equivalent to calling
|
||||||
and returning the list. That's why we say that for lists, ``+=`` is a
|
:meth:`~sequence.extend` on the list and returning the list.
|
||||||
"shorthand" for :meth:`!list.extend`::
|
That's why we say that for lists, ``+=`` is a "shorthand" for :meth:`list.extend`::
|
||||||
|
|
||||||
>>> a_list = []
|
>>> a_list = []
|
||||||
>>> a_list += [1]
|
>>> a_list += [1]
|
||||||
|
|
|
||||||
|
|
@ -1249,8 +1249,9 @@ Glossary
|
||||||
The :class:`collections.abc.Sequence` abstract base class
|
The :class:`collections.abc.Sequence` abstract base class
|
||||||
defines a much richer interface that goes beyond just
|
defines a much richer interface that goes beyond just
|
||||||
:meth:`~object.__getitem__` and :meth:`~object.__len__`, adding
|
:meth:`~object.__getitem__` and :meth:`~object.__len__`, adding
|
||||||
:meth:`!count`, :meth:`!index`, :meth:`~object.__contains__`, and
|
:meth:`~sequence.count`, :meth:`~sequence.index`,
|
||||||
:meth:`~object.__reversed__`. Types that implement this expanded
|
:meth:`~object.__contains__`, and :meth:`~object.__reversed__`.
|
||||||
|
Types that implement this expanded
|
||||||
interface can be registered explicitly using
|
interface can be registered explicitly using
|
||||||
:func:`~abc.ABCMeta.register`. For more documentation on sequence
|
:func:`~abc.ABCMeta.register`. For more documentation on sequence
|
||||||
methods generally, see
|
methods generally, see
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ The following functions are provided:
|
||||||
Insert *x* in *a* in sorted order.
|
Insert *x* in *a* in sorted order.
|
||||||
|
|
||||||
This function first runs :py:func:`~bisect.bisect_left` to locate an insertion point.
|
This function first runs :py:func:`~bisect.bisect_left` to locate an insertion point.
|
||||||
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
|
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
|
||||||
appropriate position to maintain sort order.
|
appropriate position to maintain sort order.
|
||||||
|
|
||||||
To support inserting records in a table, the *key* function (if any) is
|
To support inserting records in a table, the *key* function (if any) is
|
||||||
|
|
@ -103,7 +103,7 @@ The following functions are provided:
|
||||||
entries of *x*.
|
entries of *x*.
|
||||||
|
|
||||||
This function first runs :py:func:`~bisect.bisect_right` to locate an insertion point.
|
This function first runs :py:func:`~bisect.bisect_right` to locate an insertion point.
|
||||||
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
|
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
|
||||||
appropriate position to maintain sort order.
|
appropriate position to maintain sort order.
|
||||||
|
|
||||||
To support inserting records in a table, the *key* function (if any) is
|
To support inserting records in a table, the *key* function (if any) is
|
||||||
|
|
|
||||||
|
|
@ -264,8 +264,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
|
||||||
ABCs for read-only and mutable :term:`sequences <sequence>`.
|
ABCs for read-only and mutable :term:`sequences <sequence>`.
|
||||||
|
|
||||||
Implementation note: Some of the mixin methods, such as
|
Implementation note: Some of the mixin methods, such as
|
||||||
:meth:`~container.__iter__`, :meth:`~object.__reversed__` and :meth:`index`, make
|
:meth:`~container.__iter__`, :meth:`~object.__reversed__`,
|
||||||
repeated calls to the underlying :meth:`~object.__getitem__` method.
|
and :meth:`~sequence.index` make repeated calls to the underlying
|
||||||
|
:meth:`~object.__getitem__` method.
|
||||||
Consequently, if :meth:`~object.__getitem__` is implemented with constant
|
Consequently, if :meth:`~object.__getitem__` is implemented with constant
|
||||||
access speed, the mixin methods will have linear performance;
|
access speed, the mixin methods will have linear performance;
|
||||||
however, if the underlying method is linear (as it would be with a
|
however, if the underlying method is linear (as it would be with a
|
||||||
|
|
@ -281,8 +282,8 @@ Collections Abstract Base Classes -- Detailed Descriptions
|
||||||
Supporting the *start* and *stop* arguments is optional, but recommended.
|
Supporting the *start* and *stop* arguments is optional, but recommended.
|
||||||
|
|
||||||
.. versionchanged:: 3.5
|
.. versionchanged:: 3.5
|
||||||
The :meth:`!index` method added support for *stop* and *start*
|
The :meth:`~sequence.index` method gained support for
|
||||||
arguments.
|
the *stop* and *start* arguments.
|
||||||
|
|
||||||
.. class:: Set
|
.. class:: Set
|
||||||
MutableSet
|
MutableSet
|
||||||
|
|
|
||||||
|
|
@ -783,10 +783,10 @@ sequence of key-value pairs into a dictionary of lists:
|
||||||
|
|
||||||
When each key is encountered for the first time, it is not already in the
|
When each key is encountered for the first time, it is not already in the
|
||||||
mapping; so an entry is automatically created using the :attr:`~defaultdict.default_factory`
|
mapping; so an entry is automatically created using the :attr:`~defaultdict.default_factory`
|
||||||
function which returns an empty :class:`list`. The :meth:`!list.append`
|
function which returns an empty :class:`list`. The :meth:`list.append`
|
||||||
operation then attaches the value to the new list. When keys are encountered
|
operation then attaches the value to the new list. When keys are encountered
|
||||||
again, the look-up proceeds normally (returning the list for that key) and the
|
again, the look-up proceeds normally (returning the list for that key) and the
|
||||||
:meth:`!list.append` operation adds another value to the list. This technique is
|
:meth:`list.append` operation adds another value to the list. This technique is
|
||||||
simpler and faster than an equivalent technique using :meth:`dict.setdefault`:
|
simpler and faster than an equivalent technique using :meth:`dict.setdefault`:
|
||||||
|
|
||||||
>>> d = {}
|
>>> d = {}
|
||||||
|
|
|
||||||
|
|
@ -732,8 +732,8 @@ or both.
|
||||||
These items will be appended to the object either using
|
These items will be appended to the object either using
|
||||||
``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``.
|
``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``.
|
||||||
This is primarily used for list subclasses, but may be used by other
|
This is primarily used for list subclasses, but may be used by other
|
||||||
classes as long as they have
|
classes as long as they have :meth:`~sequence.append`
|
||||||
:ref:`append and extend methods <typesseq-common>` with
|
and :meth:`~sequence.extend` methods with
|
||||||
the appropriate signature. (Whether :meth:`!append` or :meth:`!extend` is
|
the appropriate signature. (Whether :meth:`!append` or :meth:`!extend` is
|
||||||
used depends on which pickle protocol version is used as well as the number
|
used depends on which pickle protocol version is used as well as the number
|
||||||
of items to append, so both must be supported.)
|
of items to append, so both must be supported.)
|
||||||
|
|
|
||||||
|
|
@ -1000,8 +1000,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
|
||||||
pair: slice; operation
|
pair: slice; operation
|
||||||
pair: operator; in
|
pair: operator; in
|
||||||
pair: operator; not in
|
pair: operator; not in
|
||||||
single: count() (sequence method)
|
|
||||||
single: index() (sequence method)
|
|
||||||
|
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
| Operation | Result | Notes |
|
| Operation | Result | Notes |
|
||||||
|
|
@ -1018,7 +1016,7 @@ operations have the same priority as the corresponding numeric operations. [3]_
|
||||||
| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
|
| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
|
||||||
| ``n * s`` | itself *n* times | |
|
| ``n * s`` | itself *n* times | |
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(9) |
|
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(8) |
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
|
| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
|
|
@ -1031,13 +1029,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
| ``max(s)`` | largest item of *s* | |
|
| ``max(s)`` | largest item of *s* | |
|
||||||
+--------------------------+--------------------------------+----------+
|
+--------------------------+--------------------------------+----------+
|
||||||
| ``s.index(x[, i[, j]])`` | index of the first occurrence | \(8) |
|
|
||||||
| | of *x* in *s* (at or after | |
|
|
||||||
| | index *i* and before index *j*)| |
|
|
||||||
+--------------------------+--------------------------------+----------+
|
|
||||||
| ``s.count(x)`` | total number of occurrences of | |
|
|
||||||
| | *x* in *s* | |
|
|
||||||
+--------------------------+--------------------------------+----------+
|
|
||||||
|
|
||||||
Sequences of the same type also support comparisons. In particular, tuples
|
Sequences of the same type also support comparisons. In particular, tuples
|
||||||
and lists are compared lexicographically by comparing corresponding elements.
|
and lists are compared lexicographically by comparing corresponding elements.
|
||||||
|
|
@ -1143,16 +1134,42 @@ Notes:
|
||||||
concatenation or repetition.
|
concatenation or repetition.
|
||||||
|
|
||||||
(8)
|
(8)
|
||||||
``index`` raises :exc:`ValueError` when *x* is not found in *s*.
|
|
||||||
Not all implementations support passing the additional arguments *i* and *j*.
|
|
||||||
These arguments allow efficient searching of subsections of the sequence. Passing
|
|
||||||
the extra arguments is roughly equivalent to using ``s[i:j].index(x)``, only
|
|
||||||
without copying any data and with the returned index being relative to
|
|
||||||
the start of the sequence rather than the start of the slice.
|
|
||||||
|
|
||||||
(9)
|
|
||||||
An :exc:`IndexError` is raised if *i* is outside the sequence range.
|
An :exc:`IndexError` is raised if *i* is outside the sequence range.
|
||||||
|
|
||||||
|
.. rubric:: Sequence Methods
|
||||||
|
|
||||||
|
Sequence types also support the following methods:
|
||||||
|
|
||||||
|
.. method:: list.count(value, /)
|
||||||
|
range.count(value, /)
|
||||||
|
tuple.count(value, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.count(value, /)
|
||||||
|
|
||||||
|
Return the total number of occurrences of *value* in *sequence*.
|
||||||
|
|
||||||
|
.. method:: list.index(value[, start[, stop])
|
||||||
|
range.index(value[, start[, stop])
|
||||||
|
tuple.index(value[, start[, stop])
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.index(value[, start[, stop])
|
||||||
|
|
||||||
|
Return the index of the first occurrence of *value* in *sequence*.
|
||||||
|
|
||||||
|
Raises :exc:`ValueError` if *value* is not found in *sequence*.
|
||||||
|
|
||||||
|
The *start* or *stop* arguments allow for efficient searching
|
||||||
|
of subsections of the sequence, beginning at *start* and ending at *stop*.
|
||||||
|
This is roughly equivalent to ``start + sequence[start:stop].index(value)``,
|
||||||
|
only without copying any data.
|
||||||
|
|
||||||
|
.. caution::
|
||||||
|
Not all sequence types support passing the *start* and *stop* arguments.
|
||||||
|
|
||||||
|
|
||||||
.. _typesseq-immutable:
|
.. _typesseq-immutable:
|
||||||
|
|
||||||
|
|
@ -1202,14 +1219,6 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
|
||||||
pair: subscript; assignment
|
pair: subscript; assignment
|
||||||
pair: slice; assignment
|
pair: slice; assignment
|
||||||
pair: statement; del
|
pair: statement; del
|
||||||
single: append() (sequence method)
|
|
||||||
single: clear() (sequence method)
|
|
||||||
single: copy() (sequence method)
|
|
||||||
single: extend() (sequence method)
|
|
||||||
single: insert() (sequence method)
|
|
||||||
single: pop() (sequence method)
|
|
||||||
single: remove() (sequence method)
|
|
||||||
single: reverse() (sequence method)
|
|
||||||
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
+------------------------------+--------------------------------+---------------------+
|
||||||
| Operation | Result | Notes |
|
| Operation | Result | Notes |
|
||||||
|
|
@ -1233,39 +1242,14 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
|
||||||
| ``del s[i:j:k]`` | removes the elements of | |
|
| ``del s[i:j:k]`` | removes the elements of | |
|
||||||
| | ``s[i:j:k]`` from the list | |
|
| | ``s[i:j:k]`` from the list | |
|
||||||
+------------------------------+--------------------------------+---------------------+
|
+------------------------------+--------------------------------+---------------------+
|
||||||
| ``s.append(x)`` | appends *x* to the end of the | |
|
| ``s += t`` | extends *s* with the | |
|
||||||
| | sequence (same as | |
|
| | contents of *t* (for the | |
|
||||||
| | ``s[len(s):len(s)] = [x]``) | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.clear()`` | removes all items from *s* | \(5) |
|
|
||||||
| | (same as ``del s[:]``) | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.copy()`` | creates a shallow copy of *s* | \(5) |
|
|
||||||
| | (same as ``s[:]``) | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.extend(t)`` or | extends *s* with the | |
|
|
||||||
| ``s += t`` | contents of *t* (for the | |
|
|
||||||
| | most part the same as | |
|
| | most part the same as | |
|
||||||
| | ``s[len(s):len(s)] = t``) | |
|
| | ``s[len(s):len(s)] = t``) | |
|
||||||
+------------------------------+--------------------------------+---------------------+
|
+------------------------------+--------------------------------+---------------------+
|
||||||
| ``s *= n`` | updates *s* with its contents | \(6) |
|
| ``s *= n`` | updates *s* with its contents | \(2) |
|
||||||
| | repeated *n* times | |
|
| | repeated *n* times | |
|
||||||
+------------------------------+--------------------------------+---------------------+
|
+------------------------------+--------------------------------+---------------------+
|
||||||
| ``s.insert(i, x)`` | inserts *x* into *s* at the | |
|
|
||||||
| | index given by *i* | |
|
|
||||||
| | (same as ``s[i:i] = [x]``) | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.pop()`` or ``s.pop(i)`` | retrieves the item at *i* and | \(2) |
|
|
||||||
| | also removes it from *s* | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.remove(x)`` | removes the first item from | \(3) |
|
|
||||||
| | *s* where ``s[i]`` is equal to | |
|
|
||||||
| | *x* | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
| ``s.reverse()`` | reverses the items of *s* in | \(4) |
|
|
||||||
| | place | |
|
|
||||||
+------------------------------+--------------------------------+---------------------+
|
|
||||||
|
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
|
|
@ -1273,33 +1257,106 @@ Notes:
|
||||||
If *k* is not equal to ``1``, *t* must have the same length as the slice it is replacing.
|
If *k* is not equal to ``1``, *t* must have the same length as the slice it is replacing.
|
||||||
|
|
||||||
(2)
|
(2)
|
||||||
The optional argument *i* defaults to ``-1``, so that by default the last
|
|
||||||
item is removed and returned.
|
|
||||||
|
|
||||||
(3)
|
|
||||||
:meth:`remove` raises :exc:`ValueError` when *x* is not found in *s*.
|
|
||||||
|
|
||||||
(4)
|
|
||||||
The :meth:`reverse` method modifies the sequence in place for economy of
|
|
||||||
space when reversing a large sequence. To remind users that it operates by
|
|
||||||
side effect, it does not return the reversed sequence.
|
|
||||||
|
|
||||||
(5)
|
|
||||||
:meth:`clear` and :meth:`!copy` are included for consistency with the
|
|
||||||
interfaces of mutable containers that don't support slicing operations
|
|
||||||
(such as :class:`dict` and :class:`set`). :meth:`!copy` is not part of the
|
|
||||||
:class:`collections.abc.MutableSequence` ABC, but most concrete
|
|
||||||
mutable sequence classes provide it.
|
|
||||||
|
|
||||||
.. versionadded:: 3.3
|
|
||||||
:meth:`clear` and :meth:`!copy` methods.
|
|
||||||
|
|
||||||
(6)
|
|
||||||
The value *n* is an integer, or an object implementing
|
The value *n* is an integer, or an object implementing
|
||||||
:meth:`~object.__index__`. Zero and negative values of *n* clear
|
:meth:`~object.__index__`. Zero and negative values of *n* clear
|
||||||
the sequence. Items in the sequence are not copied; they are referenced
|
the sequence. Items in the sequence are not copied; they are referenced
|
||||||
multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
|
multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
|
||||||
|
|
||||||
|
.. rubric:: Mutable Sequence Methods
|
||||||
|
|
||||||
|
Mutable sequence types also support the following methods:
|
||||||
|
|
||||||
|
.. method:: bytearray.append(value, /)
|
||||||
|
list.append(value, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.append(value, /)
|
||||||
|
|
||||||
|
Append *value* to the end of the sequence
|
||||||
|
This is equivalent to writing ``seq[len(seq):len(seq)] = [value]``.
|
||||||
|
|
||||||
|
.. method:: bytearray.clear()
|
||||||
|
list.clear()
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.clear()
|
||||||
|
|
||||||
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
|
Remove all items from *sequence*.
|
||||||
|
This is equivalent to writing ``del sequence[:]``.
|
||||||
|
|
||||||
|
.. method:: bytearray.copy()
|
||||||
|
list.copy()
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.copy()
|
||||||
|
|
||||||
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
|
Create a shallow copy of *sequence*.
|
||||||
|
This is equivalent to writing ``sequence[:]``.
|
||||||
|
|
||||||
|
.. hint:: The :meth:`!copy` method is not part of the
|
||||||
|
:class:`~collections.abc.MutableSequence` :class:`~abc.ABC`,
|
||||||
|
but most concrete mutable sequence types provide it.
|
||||||
|
|
||||||
|
.. method:: bytearray.extend(iterable, /)
|
||||||
|
list.extend(iterable, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.extend(iterable, /)
|
||||||
|
|
||||||
|
Extend *sequence* with the contents of *iterable*.
|
||||||
|
For the most part, this is the same as writing
|
||||||
|
``seq[len(seq):len(seq)] = iterable``.
|
||||||
|
|
||||||
|
.. method:: bytearray.insert(index, value, /)
|
||||||
|
list.insert(index, value, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.insert(index, value, /)
|
||||||
|
|
||||||
|
Insert *value* into *sequence* at the given *index*.
|
||||||
|
This is equivalent to writing ``sequence[index:index] = [value]``.
|
||||||
|
|
||||||
|
.. method:: bytearray.pop(index=-1, /)
|
||||||
|
list.pop(index=-1, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.pop(index=-1, /)
|
||||||
|
|
||||||
|
Retrieve the item at *index* and also removes it from *sequence*.
|
||||||
|
By default, the last item in *sequence* is removed and returned.
|
||||||
|
|
||||||
|
.. method:: bytearray.remove(value, /)
|
||||||
|
list.remove(value, /)
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.remove(value, /)
|
||||||
|
|
||||||
|
Remove the first item from *sequence* where ``sequence[i] == value``.
|
||||||
|
|
||||||
|
Raises :exc:`ValueError` if *value* is not found in *sequence*.
|
||||||
|
|
||||||
|
.. method:: bytearray.reverse()
|
||||||
|
list.reverse()
|
||||||
|
:no-contents-entry:
|
||||||
|
:no-index-entry:
|
||||||
|
:no-typesetting:
|
||||||
|
.. method:: sequence.reverse()
|
||||||
|
|
||||||
|
Reverse the items of *sequence* in place.
|
||||||
|
This method maintains economy of space when reversing a large sequence.
|
||||||
|
To remind users that it operates by side-effect, it returns ``None``.
|
||||||
|
|
||||||
|
|
||||||
.. _typesseq-list:
|
.. _typesseq-list:
|
||||||
|
|
||||||
|
|
@ -5761,9 +5818,10 @@ Methods
|
||||||
|
|
||||||
.. index:: pair: object; method
|
.. index:: pair: object; method
|
||||||
|
|
||||||
Methods are functions that are called using the attribute notation. There are
|
Methods are functions that are called using the attribute notation.
|
||||||
two flavors: :ref:`built-in methods <builtin-methods>` (such as :meth:`append`
|
There are two flavors: :ref:`built-in methods <builtin-methods>`
|
||||||
on lists) and :ref:`class instance method <instance-methods>`.
|
(such as :meth:`~list.append` on lists)
|
||||||
|
and :ref:`class instance method <instance-methods>`.
|
||||||
Built-in methods are described with the types that support them.
|
Built-in methods are described with the types that support them.
|
||||||
|
|
||||||
If you access a method (a function defined in a class namespace) through an
|
If you access a method (a function defined in a class namespace) through an
|
||||||
|
|
|
||||||
|
|
@ -3133,11 +3133,12 @@ objects. The :mod:`collections.abc` module provides a
|
||||||
:term:`abstract base class` to help create those methods from a base set of
|
:term:`abstract base class` to help create those methods from a base set of
|
||||||
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
|
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
|
||||||
:meth:`~object.__delitem__`, and :meth:`!keys`.
|
:meth:`~object.__delitem__`, and :meth:`!keys`.
|
||||||
Mutable sequences should provide methods :meth:`!append`, :meth:`!count`,
|
Mutable sequences should provide methods :meth:`~sequence.append`,
|
||||||
:meth:`!index`, :meth:`!extend`, :meth:`!insert`, :meth:`!pop`, :meth:`!remove`,
|
:meth:`~sequence.count`, :meth:`~sequence.index`, :meth:`~sequence.extend`,
|
||||||
:meth:`!reverse` and :meth:`!sort`, like Python standard :class:`list`
|
:meth:`~sequence.insert`, :meth:`~sequence.pop`, :meth:`~sequence.remove`,
|
||||||
objects. Finally,
|
:meth:`~sequence.reverse` and :meth:`~sequence.sort`,
|
||||||
sequence types should implement addition (meaning concatenation) and
|
like Python standard :class:`list` objects.
|
||||||
|
Finally, sequence types should implement addition (meaning concatenation) and
|
||||||
multiplication (meaning repetition) by defining the methods
|
multiplication (meaning repetition) by defining the methods
|
||||||
:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`,
|
:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`,
|
||||||
:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__`
|
:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__`
|
||||||
|
|
|
||||||
|
|
@ -561,7 +561,7 @@ This example, as usual, demonstrates some new Python features:
|
||||||
Different types define different methods. Methods of different types may have
|
Different types define different methods. Methods of different types may have
|
||||||
the same name without causing ambiguity. (It is possible to define your own
|
the same name without causing ambiguity. (It is possible to define your own
|
||||||
object types and methods, using *classes*, see :ref:`tut-classes`)
|
object types and methods, using *classes*, see :ref:`tut-classes`)
|
||||||
The method :meth:`!append` shown in the example is defined for list objects; it
|
The method :meth:`~list.append` shown in the example is defined for list objects; it
|
||||||
adds a new element at the end of the list. In this example it is equivalent to
|
adds a new element at the end of the list. In this example it is equivalent to
|
||||||
``result = result + [a]``, but more efficient.
|
``result = result + [a]``, but more efficient.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -142,8 +142,8 @@ Using Lists as Stacks
|
||||||
|
|
||||||
The list methods make it very easy to use a list as a stack, where the last
|
The list methods make it very easy to use a list as a stack, where the last
|
||||||
element added is the first element retrieved ("last-in, first-out"). To add an
|
element added is the first element retrieved ("last-in, first-out"). To add an
|
||||||
item to the top of the stack, use :meth:`!append`. To retrieve an item from the
|
item to the top of the stack, use :meth:`~list.append`. To retrieve an item from the
|
||||||
top of the stack, use :meth:`!pop` without an explicit index. For example::
|
top of the stack, use :meth:`~list.pop` without an explicit index. For example::
|
||||||
|
|
||||||
>>> stack = [3, 4, 5]
|
>>> stack = [3, 4, 5]
|
||||||
>>> stack.append(6)
|
>>> stack.append(6)
|
||||||
|
|
@ -340,7 +340,7 @@ The :keyword:`!del` statement
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
There is a way to remove an item from a list given its index instead of its
|
There is a way to remove an item from a list given its index instead of its
|
||||||
value: the :keyword:`del` statement. This differs from the :meth:`!pop` method
|
value: the :keyword:`del` statement. This differs from the :meth:`~list.pop` method
|
||||||
which returns a value. The :keyword:`!del` statement can also be used to remove
|
which returns a value. The :keyword:`!del` statement can also be used to remove
|
||||||
slices from a list or clear the entire list (which we did earlier by assignment
|
slices from a list or clear the entire list (which we did earlier by assignment
|
||||||
of an empty list to the slice). For example::
|
of an empty list to the slice). For example::
|
||||||
|
|
@ -500,8 +500,8 @@ any immutable type; strings and numbers can always be keys. Tuples can be used
|
||||||
as keys if they contain only strings, numbers, or tuples; if a tuple contains
|
as keys if they contain only strings, numbers, or tuples; if a tuple contains
|
||||||
any mutable object either directly or indirectly, it cannot be used as a key.
|
any mutable object either directly or indirectly, it cannot be used as a key.
|
||||||
You can't use lists as keys, since lists can be modified in place using index
|
You can't use lists as keys, since lists can be modified in place using index
|
||||||
assignments, slice assignments, or methods like :meth:`!append` and
|
assignments, slice assignments, or methods like :meth:`~list.append` and
|
||||||
:meth:`!extend`.
|
:meth:`~list.extend`.
|
||||||
|
|
||||||
It is best to think of a dictionary as a set of *key: value* pairs,
|
It is best to think of a dictionary as a set of *key: value* pairs,
|
||||||
with the requirement that the keys are unique (within one dictionary). A pair of
|
with the requirement that the keys are unique (within one dictionary). A pair of
|
||||||
|
|
|
||||||
|
|
@ -420,7 +420,7 @@ type, i.e. it is possible to change their content::
|
||||||
[1, 8, 27, 64, 125]
|
[1, 8, 27, 64, 125]
|
||||||
|
|
||||||
You can also add new items at the end of the list, by using
|
You can also add new items at the end of the list, by using
|
||||||
the :meth:`!list.append` *method* (we will see more about methods later)::
|
the :meth:`list.append` *method* (we will see more about methods later)::
|
||||||
|
|
||||||
>>> cubes.append(216) # add the cube of 6
|
>>> cubes.append(216) # add the cube of 6
|
||||||
>>> cubes.append(7 ** 3) # and the cube of 7
|
>>> cubes.append(7 ** 3) # and the cube of 7
|
||||||
|
|
|
||||||
|
|
@ -656,7 +656,8 @@ break.
|
||||||
The change which will probably break the most code is tightening up the
|
The change which will probably break the most code is tightening up the
|
||||||
arguments accepted by some methods. Some methods would take multiple arguments
|
arguments accepted by some methods. Some methods would take multiple arguments
|
||||||
and treat them as a tuple, particularly various list methods such as
|
and treat them as a tuple, particularly various list methods such as
|
||||||
:meth:`!append` and :meth:`!insert`. In earlier versions of Python, if ``L`` is
|
:meth:`~list.append` and :meth:`~list.insert`.
|
||||||
|
In earlier versions of Python, if ``L`` is
|
||||||
a list, ``L.append( 1,2 )`` appends the tuple ``(1,2)`` to the list. In Python
|
a list, ``L.append( 1,2 )`` appends the tuple ``(1,2)`` to the list. In Python
|
||||||
2.0 this causes a :exc:`TypeError` exception to be raised, with the message:
|
2.0 this causes a :exc:`TypeError` exception to be raised, with the message:
|
||||||
'append requires exactly 1 argument; 2 given'. The fix is to simply add an
|
'append requires exactly 1 argument; 2 given'. The fix is to simply add an
|
||||||
|
|
|
||||||
|
|
@ -997,9 +997,10 @@ sequence of bytes::
|
||||||
u'\u31ef \u3244'
|
u'\u31ef \u3244'
|
||||||
|
|
||||||
Byte arrays support most of the methods of string types, such as
|
Byte arrays support most of the methods of string types, such as
|
||||||
:meth:`startswith`/:meth:`endswith`, :meth:`find`/:meth:`rfind`,
|
:meth:`~bytearray.startswith`/:meth:`~bytearray.endswith`,
|
||||||
and some of the methods of lists, such as :meth:`append`,
|
:meth:`~bytearray.find`/:meth:`~bytearray.rfind`,
|
||||||
:meth:`pop`, and :meth:`reverse`.
|
and some of the methods of lists, such as :meth:`~bytearray.append`,
|
||||||
|
:meth:`~bytearray.pop`, and :meth:`~bytearray.reverse`.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|
@ -1528,8 +1529,8 @@ Some smaller changes made to the core Python language are:
|
||||||
the :exc:`StopIteration` exception will be raised. (Backported
|
the :exc:`StopIteration` exception will be raised. (Backported
|
||||||
in :issue:`2719`.)
|
in :issue:`2719`.)
|
||||||
|
|
||||||
* Tuples now have :meth:`index` and :meth:`count` methods matching the
|
* Tuples now have :meth:`~tuple.index` and :meth:`~tuple.count` methods
|
||||||
list type's :meth:`index` and :meth:`count` methods::
|
matching the list type's :meth:`~list.index` and :meth:`~list.count` methods::
|
||||||
|
|
||||||
>>> t = (0,1,2,3,4,0,1,2)
|
>>> t = (0,1,2,3,4,0,1,2)
|
||||||
>>> t.index(3)
|
>>> t.index(3)
|
||||||
|
|
|
||||||
|
|
@ -1292,7 +1292,7 @@ This section covers specific optimizations independent of the
|
||||||
(Contributed by Stefan Behnel in :gh:`68264`.)
|
(Contributed by Stefan Behnel in :gh:`68264`.)
|
||||||
|
|
||||||
* Resizing lists is streamlined for the common case,
|
* Resizing lists is streamlined for the common case,
|
||||||
speeding up :meth:`!list.append` by ≈15%
|
speeding up :meth:`list.append` by ≈15%
|
||||||
and simple :term:`list comprehension`\s by up to 20-30%
|
and simple :term:`list comprehension`\s by up to 20-30%
|
||||||
(Contributed by Dennis Sweeney in :gh:`91165`.)
|
(Contributed by Dennis Sweeney in :gh:`91165`.)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2177,7 +2177,7 @@ New Features
|
||||||
(Contributed by Sam Gross in :gh:`114329`.)
|
(Contributed by Sam Gross in :gh:`114329`.)
|
||||||
|
|
||||||
* Add the :c:func:`PyList_Extend` and :c:func:`PyList_Clear` functions,
|
* Add the :c:func:`PyList_Extend` and :c:func:`PyList_Clear` functions,
|
||||||
mirroring the Python :meth:`!list.extend` and :meth:`!list.clear` methods.
|
mirroring the Python :meth:`list.extend` and :meth:`list.clear` methods.
|
||||||
(Contributed by Victor Stinner in :gh:`111138`.)
|
(Contributed by Victor Stinner in :gh:`111138`.)
|
||||||
|
|
||||||
* Add the :c:func:`PyLong_AsInt` function.
|
* Add the :c:func:`PyLong_AsInt` function.
|
||||||
|
|
|
||||||
|
|
@ -2380,8 +2380,8 @@ Porting Python code
|
||||||
for top-level modules. E.g. ``__import__('sys', level=1)`` is now an error.
|
for top-level modules. E.g. ``__import__('sys', level=1)`` is now an error.
|
||||||
|
|
||||||
* Because :data:`sys.meta_path` and :data:`sys.path_hooks` now have finders on
|
* Because :data:`sys.meta_path` and :data:`sys.path_hooks` now have finders on
|
||||||
them by default, you will most likely want to use :meth:`!list.insert` instead
|
them by default, you will most likely want to use :meth:`list.insert` instead
|
||||||
of :meth:`!list.append` to add to those lists.
|
of :meth:`list.append` to add to those lists.
|
||||||
|
|
||||||
* Because ``None`` is now inserted into :data:`sys.path_importer_cache`, if you
|
* Because ``None`` is now inserted into :data:`sys.path_importer_cache`, if you
|
||||||
are clearing out entries in the dictionary of paths that do not have a
|
are clearing out entries in the dictionary of paths that do not have a
|
||||||
|
|
|
||||||
|
|
@ -4913,11 +4913,11 @@ Allow tuples of length 20 in the freelist to be reused.
|
||||||
.. nonce: lYKYYP
|
.. nonce: lYKYYP
|
||||||
.. section: Core and Builtins
|
.. section: Core and Builtins
|
||||||
|
|
||||||
:exc:`ValueError` messages for :meth:`!list.index`, :meth:`!range.index`,
|
:exc:`ValueError` messages for :meth:`list.index`, :meth:`range.index`,
|
||||||
:meth:`!deque.index`, :meth:`!deque.remove` and :meth:`!ShareableList.index`
|
:meth:`!deque.index`, :meth:`!deque.remove` and :meth:`!ShareableList.index`
|
||||||
no longer contain the repr of the searched value (which can be arbitrary
|
no longer contain the repr of the searched value (which can be arbitrary
|
||||||
large) and are consistent with error messages for other :meth:`!index` and
|
large) and are consistent with error messages for other :meth:`~sequence.index` and
|
||||||
:meth:`!remove` methods.
|
:meth:`~sequence.remove` methods.
|
||||||
|
|
||||||
..
|
..
|
||||||
|
|
||||||
|
|
@ -5604,7 +5604,7 @@ Using :data:`NotImplemented` in a boolean context now raises
|
||||||
.. nonce: wNMKVd
|
.. nonce: wNMKVd
|
||||||
.. section: Core and Builtins
|
.. section: Core and Builtins
|
||||||
|
|
||||||
Fix race condition in free-threaded build where :meth:`!list.extend` could
|
Fix race condition in free-threaded build where :meth:`list.extend` could
|
||||||
expose uninitialised memory to concurrent readers.
|
expose uninitialised memory to concurrent readers.
|
||||||
|
|
||||||
..
|
..
|
||||||
|
|
|
||||||
|
|
@ -671,7 +671,7 @@ Allow the JIT to remove an extra ``_TO_BOOL_BOOL`` instruction after
|
||||||
.. nonce: dNh64H
|
.. nonce: dNh64H
|
||||||
.. section: Core and Builtins
|
.. section: Core and Builtins
|
||||||
|
|
||||||
Fix crash when calling :meth:`!list.append` as an unbound method.
|
Fix crash when calling :meth:`list.append` as an unbound method.
|
||||||
|
|
||||||
..
|
..
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue