Skip to content

Commit 2fba2ed

Browse files
committed
Improve documentation for common sequence methods
1 parent 424e2ab commit 2fba2ed

File tree

20 files changed

+175
-114
lines changed

20 files changed

+175
-114
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
@@ -591,9 +591,9 @@ exhaustive test suites that exercise every line of code in a module.
591591
An appropriate testing discipline can help build large complex applications in
592592
Python as well as having interface specifications would. In fact, it can be
593593
better because an interface specification cannot test certain properties of a
594-
program. For example, the :meth:`!list.append` method is expected to add new elements
594+
program. For example, the :meth:`list.append` method is expected to add new elements
595595
to the end of some internal list; an interface specification cannot test that
596-
your :meth:`!list.append` implementation will actually do this correctly, but it's
596+
your :meth:`list.append` implementation will actually do this correctly, but it's
597597
trivial to check this property in a test suite.
598598

599599
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
@@ -1251,8 +1251,9 @@ Glossary
12511251
The :class:`collections.abc.Sequence` abstract base class
12521252
defines a much richer interface that goes beyond just
12531253
:meth:`~object.__getitem__` and :meth:`~object.__len__`, adding
1254-
:meth:`!count`, :meth:`!index`, :meth:`~object.__contains__`, and
1255-
:meth:`~object.__reversed__`. Types that implement this expanded
1254+
:meth:`~sequence.count`, :meth:`~sequence.index`,
1255+
:meth:`~object.__contains__`, and :meth:`~object.__reversed__`.
1256+
Types that implement this expanded
12561257
interface can be registered explicitly using
12571258
:func:`~abc.ABCMeta.register`. For more documentation on sequence
12581259
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: 125 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ operations have the same priority as the corresponding numeric operations. [3]_
10181018
| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
10191019
| ``n * s`` | itself *n* times | |
10201020
+--------------------------+--------------------------------+----------+
1021-
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(9) |
1021+
| ``s[i]`` | *i*\ th item of *s*, origin 0 | (3)(8) |
10221022
+--------------------------+--------------------------------+----------+
10231023
| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
10241024
+--------------------------+--------------------------------+----------+
@@ -1031,13 +1031,36 @@ operations have the same priority as the corresponding numeric operations. [3]_
10311031
+--------------------------+--------------------------------+----------+
10321032
| ``max(s)`` | largest item of *s* | |
10331033
+--------------------------+--------------------------------+----------+
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-
+--------------------------+--------------------------------+----------+
1034+
1035+
.. method:: list.count(value, /)
1036+
range.count(value, /)
1037+
tuple.count(value, /)
1038+
:no-contents-entry:
1039+
:no-index-entry:
1040+
:no-typesetting:
1041+
.. method:: sequence.count(value, /)
1042+
1043+
Return the total number of occurrences of *value* in *sequence*.
1044+
1045+
.. method:: list.index(value, start=0, stop=None, /)
1046+
range.index(value, start=0, stop=None, /)
1047+
tuple.index(value, start=0, stop=None, /)
1048+
:no-contents-entry:
1049+
:no-index-entry:
1050+
:no-typesetting:
1051+
.. method:: sequence.index(value, start=0, stop=None, /)
1052+
1053+
Return the index of the first occurrence of *value* in *sequence*.
1054+
1055+
Raises :exc:`ValueError` if *value* is not found in *sequence*.
1056+
1057+
The *start* or *stop* arguments allow for efficient searching
1058+
of subsections of the sequence, beginning at *start* and ending at *stop*.
1059+
This is roughly equivalent to ``start + sequence[start:stop].index(value)``,
1060+
only without copying any data.
1061+
1062+
.. caution::
1063+
Not all sequence types support passing the *start* and *stop* arguments.
10411064

10421065
Sequences of the same type also support comparisons. In particular, tuples
10431066
and lists are compared lexicographically by comparing corresponding elements.
@@ -1143,14 +1166,6 @@ Notes:
11431166
concatenation or repetition.
11441167

11451168
(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)
11541169
An :exc:`IndexError` is raised if *i* is outside the sequence range.
11551170

11561171

@@ -1233,38 +1248,101 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
12331248
| ``del s[i:j:k]`` | removes the elements of | |
12341249
| | ``s[i:j:k]`` from the list | |
12351250
+------------------------------+--------------------------------+---------------------+
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 | |
1251+
| ``s += t`` | extends *s* with the | |
1252+
| | contents of *t* (for the | |
12481253
| | most part the same as | |
12491254
| | ``s[len(s):len(s)] = t``) | |
12501255
+------------------------------+--------------------------------+---------------------+
1251-
| ``s *= n`` | updates *s* with its contents | \(6) |
1256+
| ``s *= n`` | updates *s* with its contents | \(2) |
12521257
| | repeated *n* times | |
12531258
+------------------------------+--------------------------------+---------------------+
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-
+------------------------------+--------------------------------+---------------------+
1259+
1260+
.. method:: bytearray.append(value, /)
1261+
list.append(value, /)
1262+
:no-contents-entry:
1263+
:no-index-entry:
1264+
:no-typesetting:
1265+
.. method:: sequence.append(value, /)
1266+
1267+
Append *value* to the end of the sequence
1268+
This is equivalent to writing ``seq[len(seq):len(seq)] = value``.
1269+
1270+
.. method:: bytearray.clear()
1271+
list.clear()
1272+
:no-contents-entry:
1273+
:no-index-entry:
1274+
:no-typesetting:
1275+
.. method:: sequence.clear()
1276+
1277+
Remove all items from *sequence*.
1278+
This is equivalent to writing ``del sequence[:]``.
1279+
1280+
.. method:: bytearray.copy()
1281+
list.copy()
1282+
:no-contents-entry:
1283+
:no-index-entry:
1284+
:no-typesetting:
1285+
.. method:: sequence.copy()
1286+
1287+
Create a shallow copy of *sequence*.
1288+
This is equivalent to writing ``sequence[:]``.
1289+
1290+
.. hint:: The :meth:`!copy` method is not part of the
1291+
:class:`~collections.abc.MutableSequence` :class:`~abc.ABC`,
1292+
but most concrete mutable sequence types provide it.
1293+
1294+
.. method:: bytearray.extend(iterable, /)
1295+
list.extend(iterable, /)
1296+
:no-contents-entry:
1297+
:no-index-entry:
1298+
:no-typesetting:
1299+
.. method:: sequence.extend(iterable, /)
1300+
1301+
Extend *sequence* with the contents of *iterable*.
1302+
For the most part, this is the same as writing
1303+
``seq[len(seq):len(seq)] = iterable``.
1304+
1305+
.. method:: bytearray.insert(index, value, /)
1306+
list.insert(index, value, /)
1307+
:no-contents-entry:
1308+
:no-index-entry:
1309+
:no-typesetting:
1310+
.. method:: sequence.insert(index, value, /)
1311+
1312+
Insert *value* into *sequence* at the given *index*.
1313+
This is equivalent to writing ``sequence[index:index] = [value]``.
1314+
1315+
.. method:: bytearray.pop(index=-1, /)
1316+
list.pop(index=-1, /)
1317+
:no-contents-entry:
1318+
:no-index-entry:
1319+
:no-typesetting:
1320+
.. method:: sequence.pop(index=-1, /)
1321+
1322+
Retrieve the item at *index* and also removes it from *sequence*.
1323+
By default, the last item in *sequence* is removed and returned.
1324+
1325+
.. method:: bytearray.remove(value, /)
1326+
list.remove(value, /)
1327+
:no-contents-entry:
1328+
:no-index-entry:
1329+
:no-typesetting:
1330+
.. method:: sequence.remove(value, /)
1331+
1332+
Remove the first item from *sequence* where ``sequence[i] == value``.
1333+
1334+
Raises :exc:`ValueError` if *value* is not found in *sequence*.
1335+
1336+
.. method:: bytearray.reverse()
1337+
list.reverse()
1338+
:no-contents-entry:
1339+
:no-index-entry:
1340+
:no-typesetting:
1341+
.. method:: sequence.reverse()
1342+
1343+
Reverse the items of *sequence* in place.
1344+
This method maintains economy of space when reversing a large sequence.
1345+
To remind users that it operates by side-effect, it returns ``None``.
12681346

12691347

12701348
Notes:
@@ -1273,28 +1351,6 @@ Notes:
12731351
If *k* is not equal to ``1``, *t* must have the same length as the slice it is replacing.
12741352

12751353
(2)
1276-
The optional argument *i* defaults to ``-1``, so that by default the last
1277-
item is removed and returned.
1278-
1279-
(3)
1280-
:meth:`remove` raises :exc:`ValueError` when *x* is not found in *s*.
1281-
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.
1286-
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.
1293-
1294-
.. versionadded:: 3.3
1295-
:meth:`clear` and :meth:`!copy` methods.
1296-
1297-
(6)
12981354
The value *n* is an integer, or an object implementing
12991355
:meth:`~object.__index__`. Zero and negative values of *n* clear
13001356
the sequence. Items in the sequence are not copied; they are referenced
@@ -5761,9 +5817,10 @@ Methods
57615817

57625818
.. index:: pair: object; method
57635819

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>`.
5820+
Methods are functions that are called using the attribute notation.
5821+
There are two flavors: :ref:`built-in methods <builtin-methods>`
5822+
(such as :meth:`~list.append` on lists)
5823+
and :ref:`class instance method <instance-methods>`.
57675824
Built-in methods are described with the types that support them.
57685825

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

Doc/reference/datamodel.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,11 +3135,12 @@ objects. The :mod:`collections.abc` module provides a
31353135
:term:`abstract base class` to help create those methods from a base set of
31363136
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
31373137
:meth:`~object.__delitem__`, and :meth:`!keys`.
3138-
Mutable sequences should provide methods :meth:`!append`, :meth:`!count`,
3139-
:meth:`!index`, :meth:`!extend`, :meth:`!insert`, :meth:`!pop`, :meth:`!remove`,
3140-
:meth:`!reverse` and :meth:`!sort`, like Python standard :class:`list`
3141-
objects. Finally,
3142-
sequence types should implement addition (meaning concatenation) and
3138+
Mutable sequences should provide methods :meth:`~sequence.append`,
3139+
:meth:`~sequence.count`, :meth:`~sequence.index`, :meth:`~sequence.extend`,
3140+
:meth:`~sequence.insert`, :meth:`~sequence.pop`, :meth:`~sequence.remove`,
3141+
:meth:`~sequence.reverse` and :meth:`~sequence.sort`,
3142+
like Python standard :class:`list` objects.
3143+
Finally, sequence types should implement addition (meaning concatenation) and
31433144
multiplication (meaning repetition) by defining the methods
31443145
:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`,
31453146
:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__`

0 commit comments

Comments
 (0)