Skip to content

Commit 0089958

Browse files
encukoublaisepStanFromIreland
authored andcommitted
gh-141984: Reword and reorganize Subscription (and Slicing) docs (GH-141985)
(cherry picked from commit e423e0c) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Blaise Pabon <blaise@gmail.com> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent fddd858 commit 0089958

File tree

5 files changed

+283
-156
lines changed

5 files changed

+283
-156
lines changed

Doc/glossary.rst

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,19 @@ Glossary
786786
An object that both finds and loads a module; both a
787787
:term:`finder` and :term:`loader` object.
788788

789+
index
790+
A numeric value that represents the position of an element in
791+
a :term:`sequence`.
792+
793+
In Python, indexing starts at zero.
794+
For example, ``things[0]`` names the *first* element of ``things``;
795+
``things[1]`` names the second one.
796+
797+
In some contexts, Python allows negative indexes for counting from the
798+
end of a sequence, and indexing using :term:`slices <slice>`.
799+
800+
See also :term:`subscript`.
801+
789802
interactive
790803
Python has an interactive interpreter which means you can enter
791804
statements and expressions at the interpreter prompt, immediately
@@ -863,6 +876,9 @@ Glossary
863876
CPython does not guarantee :term:`thread-safe` behavior of iterator
864877
operations.
865878

879+
key
880+
A value that identifies an entry in a :term:`mapping`.
881+
See also :term:`subscript`.
866882

867883
key function
868884
A key function or collation function is a callable that returns a value
@@ -1417,10 +1433,11 @@ Glossary
14171433
chosen based on the type of a single argument.
14181434

14191435
slice
1420-
An object usually containing a portion of a :term:`sequence`. A slice is
1421-
created using the subscript notation, ``[]`` with colons between numbers
1422-
when several are given, such as in ``variable_name[1:3:5]``. The bracket
1423-
(subscript) notation uses :class:`slice` objects internally.
1436+
An object of type :class:`slice`, used to describe a portion of
1437+
a :term:`sequence`.
1438+
A slice object is created when using the :ref:`slicing <slicings>` form
1439+
of :ref:`subscript notation <subscriptions>`, with colons inside square
1440+
brackets, such as in ``variable_name[1:3:5]``.
14241441

14251442
soft deprecated
14261443
A soft deprecated API should not be used in new code,
@@ -1478,6 +1495,14 @@ Glossary
14781495

14791496
See also :term:`borrowed reference`.
14801497

1498+
subscript
1499+
The expression in square brackets of a
1500+
:ref:`subscription expression <subscriptions>`, for example,
1501+
the ``3`` in ``items[3]``.
1502+
Usually used to select an element of a container.
1503+
Also called a :term:`key` when subscripting a :term:`mapping`,
1504+
or an :term:`index` when subscripting a :term:`sequence`.
1505+
14811506
synchronization primitive
14821507
A basic building block for coordinating (synchronizing) the execution of
14831508
multiple threads to ensure :term:`thread-safe` access to shared resources.

Doc/library/functions.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,19 +1843,19 @@ are always available. They are listed here in alphabetical order.
18431843
``range(start, stop, step)``. The *start* and *step* arguments default to
18441844
``None``.
18451845

1846-
Slice objects have read-only data attributes :attr:`!start`,
1847-
:attr:`!stop`, and :attr:`!step` which merely return the argument
1848-
values (or their default). They have no other explicit functionality;
1849-
however, they are used by NumPy and other third-party packages.
1846+
Slice objects are also generated when :ref:`slicing syntax <slicings>`
1847+
is used. For example: ``a[start:stop:step]`` or ``a[start:stop, i]``.
1848+
1849+
See :func:`itertools.islice` for an alternate version that returns an
1850+
:term:`iterator`.
18501851

18511852
.. attribute:: slice.start
1852-
.. attribute:: slice.stop
1853-
.. attribute:: slice.step
1853+
slice.stop
1854+
slice.step
18541855

1855-
Slice objects are also generated when extended indexing syntax is used. For
1856-
example: ``a[start:stop:step]`` or ``a[start:stop, i]``. See
1857-
:func:`itertools.islice` for an alternate version that returns an
1858-
:term:`iterator`.
1856+
These read-only attributes are set to the argument values
1857+
(or their default). They have no other explicit functionality;
1858+
however, they are used by NumPy and other third-party packages.
18591859

18601860
.. versionchanged:: 3.12
18611861
Slice objects are now :term:`hashable` (provided :attr:`~slice.start`,

Doc/reference/datamodel.rst

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ floating-point numbers. The same caveats apply as for floating-point numbers.
290290
The real and imaginary parts of a complex number ``z`` can be retrieved through
291291
the read-only attributes ``z.real`` and ``z.imag``.
292292

293+
.. _datamodel-sequences:
293294

294295
Sequences
295296
---------
@@ -309,12 +310,25 @@ including built-in sequences, interpret negative subscripts by adding the
309310
sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
310311
item of sequence a with length ``n``.
311312

312-
.. index:: single: slicing
313+
The resulting value must be a nonnegative integer less than the number of items
314+
in the sequence. If it is not, an :exc:`IndexError` is raised.
313315

314-
Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
315-
that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
316-
sequence of the same type. The comment above about negative indexes also applies
316+
.. index::
317+
single: slicing
318+
single: start (slice object attribute)
319+
single: stop (slice object attribute)
320+
single: step (slice object attribute)
321+
322+
Sequences also support slicing: ``a[start:stop]`` selects all items with index *k* such
323+
that *start* ``<=`` *k* ``<`` *stop*. When used as an expression, a slice is a
324+
sequence of the same type. The comment above about negative subscripts also applies
317325
to negative slice positions.
326+
Note that no error is raised if a slice position is less than zero or larger
327+
than the length of the sequence.
328+
329+
If *start* is missing or :data:`None`, slicing behaves as if *start* was zero.
330+
If *stop* is missing or ``None``, slicing behaves as if *stop* was equal to
331+
the length of the sequence.
318332

319333
Some sequences also support "extended slicing" with a third "step" parameter:
320334
``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*
@@ -345,17 +359,22 @@ Strings
345359
pair: built-in function; chr
346360
pair: built-in function; ord
347361
single: character
348-
single: integer
362+
pair: string; item
349363
single: Unicode
350364

351-
A string is a sequence of values that represent Unicode code points.
352-
All the code points in the range ``U+0000 - U+10FFFF`` can be
353-
represented in a string. Python doesn't have a :c:expr:`char` type;
354-
instead, every code point in the string is represented as a string
355-
object with length ``1``. The built-in function :func:`ord`
365+
A string (:class:`str`) is a sequence of values that represent
366+
:dfn:`characters`, or more formally, *Unicode code points*.
367+
All the code points in the range ``0`` to ``0x10FFFF`` can be
368+
represented in a string.
369+
370+
Python doesn't have a dedicated *character* type.
371+
Instead, every code point in the string is represented as a string
372+
object with length ``1``.
373+
374+
The built-in function :func:`ord`
356375
converts a code point from its string form to an integer in the
357-
range ``0 - 10FFFF``; :func:`chr` converts an integer in the range
358-
``0 - 10FFFF`` to the corresponding length ``1`` string object.
376+
range ``0`` to ``0x10FFFF``; :func:`chr` converts an integer in the range
377+
``0`` to ``0x10FFFF`` to the corresponding length ``1`` string object.
359378
:meth:`str.encode` can be used to convert a :class:`str` to
360379
:class:`bytes` using the given text encoding, and
361380
:meth:`bytes.decode` can be used to achieve the opposite.
@@ -366,7 +385,7 @@ Tuples
366385
pair: singleton; tuple
367386
pair: empty; tuple
368387

369-
The items of a tuple are arbitrary Python objects. Tuples of two or
388+
The items of a :class:`tuple` are arbitrary Python objects. Tuples of two or
370389
more items are formed by comma-separated lists of expressions. A tuple
371390
of one item (a 'singleton') can be formed by affixing a comma to an
372391
expression (an expression by itself does not create a tuple, since
@@ -376,7 +395,7 @@ Tuples
376395
Bytes
377396
.. index:: bytes, byte
378397

379-
A bytes object is an immutable array. The items are 8-bit bytes,
398+
A :class:`bytes` object is an immutable array. The items are 8-bit bytes,
380399
represented by integers in the range 0 <= x < 256. Bytes literals
381400
(like ``b'abc'``) and the built-in :func:`bytes` constructor
382401
can be used to create bytes objects. Also, bytes objects can be
@@ -461,6 +480,8 @@ Frozen sets
461480
a dictionary key.
462481

463482

483+
.. _datamodel-mappings:
484+
464485
Mappings
465486
--------
466487

@@ -3220,28 +3241,39 @@ through the object's keys; for sequences, it should iterate through the values.
32203241
and so forth. Missing slice items are always filled in with ``None``.
32213242

32223243

3223-
.. method:: object.__getitem__(self, key)
3244+
.. method:: object.__getitem__(self, subscript)
3245+
3246+
Called to implement *subscription*, that is, ``self[subscript]``.
3247+
See :ref:`subscriptions` for details on the syntax.
3248+
3249+
There are two types of built-in objects that support subscription
3250+
via :meth:`!__getitem__`:
3251+
3252+
- **sequences**, where *subscript* (also called
3253+
:term:`index`) should be an integer or a :class:`slice` object.
3254+
See the :ref:`sequence documentation <datamodel-sequences>` for the expected
3255+
behavior, including handling :class:`slice` objects and negative indices.
3256+
- **mappings**, where *subscript* is also called the :term:`key`.
3257+
See :ref:`mapping documentation <datamodel-mappings>` for the expected
3258+
behavior.
32243259

3225-
Called to implement evaluation of ``self[key]``. For :term:`sequence` types,
3226-
the accepted keys should be integers. Optionally, they may support
3227-
:class:`slice` objects as well. Negative index support is also optional.
3228-
If *key* is
3229-
of an inappropriate type, :exc:`TypeError` may be raised; if *key* is a value
3230-
outside the set of indexes for the sequence (after any special
3231-
interpretation of negative values), :exc:`IndexError` should be raised. For
3232-
:term:`mapping` types, if *key* is missing (not in the container),
3233-
:exc:`KeyError` should be raised.
3260+
If *subscript* is of an inappropriate type, :meth:`!__getitem__`
3261+
should raise :exc:`TypeError`.
3262+
If *subscript* has an inappropriate value, :meth:`!__getitem__`
3263+
should raise an :exc:`LookupError` or one of its subclasses
3264+
(:exc:`IndexError` for sequences; :exc:`KeyError` for mappings).
32343265

32353266
.. note::
32363267

3237-
:keyword:`for` loops expect that an :exc:`IndexError` will be raised for
3238-
illegal indexes to allow proper detection of the end of the sequence.
3268+
The sequence iteration protocol (used, for example, in :keyword:`for`
3269+
loops), expects that an :exc:`IndexError` will be raised for illegal
3270+
indexes to allow proper detection of the end of a sequence.
32393271

32403272
.. note::
32413273

3242-
When :ref:`subscripting<subscriptions>` a *class*, the special
3274+
When :ref:`subscripting <subscriptions>` a *class*, the special
32433275
class method :meth:`~object.__class_getitem__` may be called instead of
3244-
``__getitem__()``. See :ref:`classgetitem-versus-getitem` for more
3276+
:meth:`!__getitem__`. See :ref:`classgetitem-versus-getitem` for more
32453277
details.
32463278

32473279

0 commit comments

Comments
 (0)