Skip to content

Commit f153e32

Browse files
miss-islingtonAA-Turnerserhiy-storchaka
authored
[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 8ed1d53) Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent cddb7e6 commit f153e32

20 files changed

+185
-123
lines changed

Doc/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@
245245
('py:attr', '__annotations__'),
246246
('py:meth', '__missing__'),
247247
('py:attr', '__wrapped__'),
248-
('py:meth', 'index'), # list.index, tuple.index, etc.
249248
]
250249

251250
# gh-106948: Copy standard C types declared in the "c:type" domain and C

Doc/faq/design.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,9 @@ exhaustive test suites that exercise every line of code in a module.
589589
An appropriate testing discipline can help build large complex applications in
590590
Python as well as having interface specifications would. In fact, it can be
591591
better because an interface specification cannot test certain properties of a
592-
program. For example, the :meth:`!list.append` method is expected to add new elements
592+
program. For example, the :meth:`list.append` method is expected to add new elements
593593
to the end of some internal list; an interface specification cannot test that
594-
your :meth:`!list.append` implementation will actually do this correctly, but it's
594+
your :meth:`list.append` implementation will actually do this correctly, but it's
595595
trivial to check this property in a test suite.
596596

597597
Writing test suites is very helpful, and you might want to design your code to

Doc/faq/programming.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ There are two factors that produce this result:
454454
(the list), and both ``x`` and ``y`` refer to it.
455455
2) Lists are :term:`mutable`, which means that you can change their content.
456456

457-
After the call to :meth:`!append`, the content of the mutable object has
457+
After the call to :meth:`~sequence.append`, the content of the mutable object has
458458
changed from ``[]`` to ``[10]``. Since both the variables refer to the same
459459
object, using either name accesses the modified value ``[10]``.
460460

@@ -1397,9 +1397,9 @@ To see why this happens, you need to know that (a) if an object implements an
13971397
:meth:`~object.__iadd__` magic method, it gets called when the ``+=`` augmented
13981398
assignment
13991399
is executed, and its return value is what gets used in the assignment statement;
1400-
and (b) for lists, :meth:`!__iadd__` is equivalent to calling :meth:`!extend` on the list
1401-
and returning the list. That's why we say that for lists, ``+=`` is a
1402-
"shorthand" for :meth:`!list.extend`::
1400+
and (b) for lists, :meth:`!__iadd__` is equivalent to calling
1401+
:meth:`~sequence.extend` on the list and returning the list.
1402+
That's why we say that for lists, ``+=`` is a "shorthand" for :meth:`list.extend`::
14031403

14041404
>>> a_list = []
14051405
>>> a_list += [1]

Doc/glossary.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,9 @@ Glossary
12491249
The :class:`collections.abc.Sequence` abstract base class
12501250
defines a much richer interface that goes beyond just
12511251
:meth:`~object.__getitem__` and :meth:`~object.__len__`, adding
1252-
:meth:`!count`, :meth:`!index`, :meth:`~object.__contains__`, and
1253-
:meth:`~object.__reversed__`. Types that implement this expanded
1252+
:meth:`~sequence.count`, :meth:`~sequence.index`,
1253+
:meth:`~object.__contains__`, and :meth:`~object.__reversed__`.
1254+
Types that implement this expanded
12541255
interface can be registered explicitly using
12551256
:func:`~abc.ABCMeta.register`. For more documentation on sequence
12561257
methods generally, see

Doc/library/bisect.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The following functions are provided:
8383
Insert *x* in *a* in sorted order.
8484

8585
This function first runs :py:func:`~bisect.bisect_left` to locate an insertion point.
86-
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
86+
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
8787
appropriate position to maintain sort order.
8888

8989
To support inserting records in a table, the *key* function (if any) is
@@ -103,7 +103,7 @@ The following functions are provided:
103103
entries of *x*.
104104

105105
This function first runs :py:func:`~bisect.bisect_right` to locate an insertion point.
106-
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
106+
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
107107
appropriate position to maintain sort order.
108108

109109
To support inserting records in a table, the *key* function (if any) is

Doc/library/collections.abc.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
264264
ABCs for read-only and mutable :term:`sequences <sequence>`.
265265

266266
Implementation note: Some of the mixin methods, such as
267-
:meth:`~container.__iter__`, :meth:`~object.__reversed__` and :meth:`index`, make
268-
repeated calls to the underlying :meth:`~object.__getitem__` method.
267+
:meth:`~container.__iter__`, :meth:`~object.__reversed__`,
268+
and :meth:`~sequence.index` make repeated calls to the underlying
269+
:meth:`~object.__getitem__` method.
269270
Consequently, if :meth:`~object.__getitem__` is implemented with constant
270271
access speed, the mixin methods will have linear performance;
271272
however, if the underlying method is linear (as it would be with a
@@ -281,8 +282,8 @@ Collections Abstract Base Classes -- Detailed Descriptions
281282
Supporting the *start* and *stop* arguments is optional, but recommended.
282283

283284
.. versionchanged:: 3.5
284-
The :meth:`!index` method added support for *stop* and *start*
285-
arguments.
285+
The :meth:`~sequence.index` method gained support for
286+
the *stop* and *start* arguments.
286287

287288
.. class:: Set
288289
MutableSet

Doc/library/collections.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,10 @@ sequence of key-value pairs into a dictionary of lists:
783783

784784
When each key is encountered for the first time, it is not already in the
785785
mapping; so an entry is automatically created using the :attr:`~defaultdict.default_factory`
786-
function which returns an empty :class:`list`. The :meth:`!list.append`
786+
function which returns an empty :class:`list`. The :meth:`list.append`
787787
operation then attaches the value to the new list. When keys are encountered
788788
again, the look-up proceeds normally (returning the list for that key) and the
789-
:meth:`!list.append` operation adds another value to the list. This technique is
789+
:meth:`list.append` operation adds another value to the list. This technique is
790790
simpler and faster than an equivalent technique using :meth:`dict.setdefault`:
791791

792792
>>> d = {}

Doc/library/pickle.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ or both.
732732
These items will be appended to the object either using
733733
``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``.
734734
This is primarily used for list subclasses, but may be used by other
735-
classes as long as they have
736-
:ref:`append and extend methods <typesseq-common>` with
735+
classes as long as they have :meth:`~sequence.append`
736+
and :meth:`~sequence.extend` methods with
737737
the appropriate signature. (Whether :meth:`!append` or :meth:`!extend` is
738738
used depends on which pickle protocol version is used as well as the number
739739
of items to append, so both must be supported.)

Doc/library/stdtypes.rst

Lines changed: 135 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
10001000
pair: slice; operation
10011001
pair: operator; in
10021002
pair: operator; not in
1003-
single: count() (sequence method)
1004-
single: index() (sequence method)
10051003

10061004
+--------------------------+--------------------------------+----------+
10071005
| Operation | Result | Notes |
@@ -1018,7 +1016,7 @@ operations have the same priority as the corresponding numeric operations. [3]_
10181016
| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
10191017
| ``n * s`` | itself *n* times | |
10201018
+--------------------------+--------------------------------+----------+
1021-
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(9) |
1019+
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(8) |
10221020
+--------------------------+--------------------------------+----------+
10231021
| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
10241022
+--------------------------+--------------------------------+----------+
@@ -1031,13 +1029,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
10311029
+--------------------------+--------------------------------+----------+
10321030
| ``max(s)`` | largest item of *s* | |
10331031
+--------------------------+--------------------------------+----------+
1034-
| ``s.index(x[, i[, j]])`` | index of the first occurrence | \(8) |
1035-
| | of *x* in *s* (at or after | |
1036-
| | index *i* and before index *j*)| |
1037-
+--------------------------+--------------------------------+----------+
1038-
| ``s.count(x)`` | total number of occurrences of | |
1039-
| | *x* in *s* | |
1040-
+--------------------------+--------------------------------+----------+
10411032

10421033
Sequences of the same type also support comparisons. In particular, tuples
10431034
and lists are compared lexicographically by comparing corresponding elements.
@@ -1143,16 +1134,42 @@ Notes:
11431134
concatenation or repetition.
11441135

11451136
(8)
1146-
``index`` raises :exc:`ValueError` when *x* is not found in *s*.
1147-
Not all implementations support passing the additional arguments *i* and *j*.
1148-
These arguments allow efficient searching of subsections of the sequence. Passing
1149-
the extra arguments is roughly equivalent to using ``s[i:j].index(x)``, only
1150-
without copying any data and with the returned index being relative to
1151-
the start of the sequence rather than the start of the slice.
1152-
1153-
(9)
11541137
An :exc:`IndexError` is raised if *i* is outside the sequence range.
11551138

1139+
.. rubric:: Sequence Methods
1140+
1141+
Sequence types also support the following methods:
1142+
1143+
.. method:: list.count(value, /)
1144+
range.count(value, /)
1145+
tuple.count(value, /)
1146+
:no-contents-entry:
1147+
:no-index-entry:
1148+
:no-typesetting:
1149+
.. method:: sequence.count(value, /)
1150+
1151+
Return the total number of occurrences of *value* in *sequence*.
1152+
1153+
.. method:: list.index(value[, start[, stop])
1154+
range.index(value[, start[, stop])
1155+
tuple.index(value[, start[, stop])
1156+
:no-contents-entry:
1157+
:no-index-entry:
1158+
:no-typesetting:
1159+
.. method:: sequence.index(value[, start[, stop])
1160+
1161+
Return the index of the first occurrence of *value* in *sequence*.
1162+
1163+
Raises :exc:`ValueError` if *value* is not found in *sequence*.
1164+
1165+
The *start* or *stop* arguments allow for efficient searching
1166+
of subsections of the sequence, beginning at *start* and ending at *stop*.
1167+
This is roughly equivalent to ``start + sequence[start:stop].index(value)``,
1168+
only without copying any data.
1169+
1170+
.. caution::
1171+
Not all sequence types support passing the *start* and *stop* arguments.
1172+
11561173

11571174
.. _typesseq-immutable:
11581175

@@ -1202,14 +1219,6 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
12021219
pair: subscript; assignment
12031220
pair: slice; assignment
12041221
pair: statement; del
1205-
single: append() (sequence method)
1206-
single: clear() (sequence method)
1207-
single: copy() (sequence method)
1208-
single: extend() (sequence method)
1209-
single: insert() (sequence method)
1210-
single: pop() (sequence method)
1211-
single: remove() (sequence method)
1212-
single: reverse() (sequence method)
12131222

12141223
+------------------------------+--------------------------------+---------------------+
12151224
| Operation | Result | Notes |
@@ -1233,72 +1242,120 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
12331242
| ``del s[i:j:k]`` | removes the elements of | |
12341243
| | ``s[i:j:k]`` from the list | |
12351244
+------------------------------+--------------------------------+---------------------+
1236-
| ``s.append(x)`` | appends *x* to the end of the | |
1237-
| | sequence (same as | |
1238-
| | ``s[len(s):len(s)] = [x]``) | |
1239-
+------------------------------+--------------------------------+---------------------+
1240-
| ``s.clear()`` | removes all items from *s* | \(5) |
1241-
| | (same as ``del s[:]``) | |
1242-
+------------------------------+--------------------------------+---------------------+
1243-
| ``s.copy()`` | creates a shallow copy of *s* | \(5) |
1244-
| | (same as ``s[:]``) | |
1245-
+------------------------------+--------------------------------+---------------------+
1246-
| ``s.extend(t)`` or | extends *s* with the | |
1247-
| ``s += t`` | contents of *t* (for the | |
1245+
| ``s += t`` | extends *s* with the | |
1246+
| | contents of *t* (for the | |
12481247
| | most part the same as | |
12491248
| | ``s[len(s):len(s)] = t``) | |
12501249
+------------------------------+--------------------------------+---------------------+
1251-
| ``s *= n`` | updates *s* with its contents | \(6) |
1250+
| ``s *= n`` | updates *s* with its contents | \(2) |
12521251
| | repeated *n* times | |
12531252
+------------------------------+--------------------------------+---------------------+
1254-
| ``s.insert(i, x)`` | inserts *x* into *s* at the | |
1255-
| | index given by *i* | |
1256-
| | (same as ``s[i:i] = [x]``) | |
1257-
+------------------------------+--------------------------------+---------------------+
1258-
| ``s.pop()`` or ``s.pop(i)`` | retrieves the item at *i* and | \(2) |
1259-
| | also removes it from *s* | |
1260-
+------------------------------+--------------------------------+---------------------+
1261-
| ``s.remove(x)`` | removes the first item from | \(3) |
1262-
| | *s* where ``s[i]`` is equal to | |
1263-
| | *x* | |
1264-
+------------------------------+--------------------------------+---------------------+
1265-
| ``s.reverse()`` | reverses the items of *s* in | \(4) |
1266-
| | place | |
1267-
+------------------------------+--------------------------------+---------------------+
1268-
12691253

12701254
Notes:
12711255

12721256
(1)
12731257
If *k* is not equal to ``1``, *t* must have the same length as the slice it is replacing.
12741258

12751259
(2)
1276-
The optional argument *i* defaults to ``-1``, so that by default the last
1277-
item is removed and returned.
1260+
The value *n* is an integer, or an object implementing
1261+
:meth:`~object.__index__`. Zero and negative values of *n* clear
1262+
the sequence. Items in the sequence are not copied; they are referenced
1263+
multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
12781264

1279-
(3)
1280-
:meth:`remove` raises :exc:`ValueError` when *x* is not found in *s*.
1265+
.. rubric:: Mutable Sequence Methods
12811266

1282-
(4)
1283-
The :meth:`reverse` method modifies the sequence in place for economy of
1284-
space when reversing a large sequence. To remind users that it operates by
1285-
side effect, it does not return the reversed sequence.
1267+
Mutable sequence types also support the following methods:
12861268

1287-
(5)
1288-
:meth:`clear` and :meth:`!copy` are included for consistency with the
1289-
interfaces of mutable containers that don't support slicing operations
1290-
(such as :class:`dict` and :class:`set`). :meth:`!copy` is not part of the
1291-
:class:`collections.abc.MutableSequence` ABC, but most concrete
1292-
mutable sequence classes provide it.
1269+
.. method:: bytearray.append(value, /)
1270+
list.append(value, /)
1271+
:no-contents-entry:
1272+
:no-index-entry:
1273+
:no-typesetting:
1274+
.. method:: sequence.append(value, /)
1275+
1276+
Append *value* to the end of the sequence
1277+
This is equivalent to writing ``seq[len(seq):len(seq)] = [value]``.
1278+
1279+
.. method:: bytearray.clear()
1280+
list.clear()
1281+
:no-contents-entry:
1282+
:no-index-entry:
1283+
:no-typesetting:
1284+
.. method:: sequence.clear()
12931285

12941286
.. versionadded:: 3.3
1295-
:meth:`clear` and :meth:`!copy` methods.
12961287

1297-
(6)
1298-
The value *n* is an integer, or an object implementing
1299-
:meth:`~object.__index__`. Zero and negative values of *n* clear
1300-
the sequence. Items in the sequence are not copied; they are referenced
1301-
multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
1288+
Remove all items from *sequence*.
1289+
This is equivalent to writing ``del sequence[:]``.
1290+
1291+
.. method:: bytearray.copy()
1292+
list.copy()
1293+
:no-contents-entry:
1294+
:no-index-entry:
1295+
:no-typesetting:
1296+
.. method:: sequence.copy()
1297+
1298+
.. versionadded:: 3.3
1299+
1300+
Create a shallow copy of *sequence*.
1301+
This is equivalent to writing ``sequence[:]``.
1302+
1303+
.. hint:: The :meth:`!copy` method is not part of the
1304+
:class:`~collections.abc.MutableSequence` :class:`~abc.ABC`,
1305+
but most concrete mutable sequence types provide it.
1306+
1307+
.. method:: bytearray.extend(iterable, /)
1308+
list.extend(iterable, /)
1309+
:no-contents-entry:
1310+
:no-index-entry:
1311+
:no-typesetting:
1312+
.. method:: sequence.extend(iterable, /)
1313+
1314+
Extend *sequence* with the contents of *iterable*.
1315+
For the most part, this is the same as writing
1316+
``seq[len(seq):len(seq)] = iterable``.
1317+
1318+
.. method:: bytearray.insert(index, value, /)
1319+
list.insert(index, value, /)
1320+
:no-contents-entry:
1321+
:no-index-entry:
1322+
:no-typesetting:
1323+
.. method:: sequence.insert(index, value, /)
1324+
1325+
Insert *value* into *sequence* at the given *index*.
1326+
This is equivalent to writing ``sequence[index:index] = [value]``.
1327+
1328+
.. method:: bytearray.pop(index=-1, /)
1329+
list.pop(index=-1, /)
1330+
:no-contents-entry:
1331+
:no-index-entry:
1332+
:no-typesetting:
1333+
.. method:: sequence.pop(index=-1, /)
1334+
1335+
Retrieve the item at *index* and also removes it from *sequence*.
1336+
By default, the last item in *sequence* is removed and returned.
1337+
1338+
.. method:: bytearray.remove(value, /)
1339+
list.remove(value, /)
1340+
:no-contents-entry:
1341+
:no-index-entry:
1342+
:no-typesetting:
1343+
.. method:: sequence.remove(value, /)
1344+
1345+
Remove the first item from *sequence* where ``sequence[i] == value``.
1346+
1347+
Raises :exc:`ValueError` if *value* is not found in *sequence*.
1348+
1349+
.. method:: bytearray.reverse()
1350+
list.reverse()
1351+
:no-contents-entry:
1352+
:no-index-entry:
1353+
:no-typesetting:
1354+
.. method:: sequence.reverse()
1355+
1356+
Reverse the items of *sequence* in place.
1357+
This method maintains economy of space when reversing a large sequence.
1358+
To remind users that it operates by side-effect, it returns ``None``.
13021359

13031360

13041361
.. _typesseq-list:
@@ -5761,9 +5818,10 @@ Methods
57615818

57625819
.. index:: pair: object; method
57635820

5764-
Methods are functions that are called using the attribute notation. There are
5765-
two flavors: :ref:`built-in methods <builtin-methods>` (such as :meth:`append`
5766-
on lists) and :ref:`class instance method <instance-methods>`.
5821+
Methods are functions that are called using the attribute notation.
5822+
There are two flavors: :ref:`built-in methods <builtin-methods>`
5823+
(such as :meth:`~list.append` on lists)
5824+
and :ref:`class instance method <instance-methods>`.
57675825
Built-in methods are described with the types that support them.
57685826

57695827
If you access a method (a function defined in a class namespace) through an

0 commit comments

Comments
 (0)