Skip to content

Commit 06108c0

Browse files
bpo-45680: Improve docs on subscriptions w.r.t. GenericAlias objects (GH-29479)
(cherry picked from commit 5073129) Co-authored-by: Alex Waygood <[email protected]>
1 parent 1e52e78 commit 06108c0

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

Doc/reference/expressions.rst

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -810,30 +810,44 @@ Subscriptions
810810
object: dictionary
811811
pair: sequence; item
812812

813-
Subscription of a sequence (string, tuple or list) or mapping (dictionary)
814-
object usually selects an item from the collection:
813+
The subscription of an instance of a :ref:`container class <sequence-types>`
814+
will generally select an element from the container. The subscription of a
815+
:term:`generic class <generic type>` will generally return a
816+
:ref:`GenericAlias <types-genericalias>` object.
815817

816818
.. productionlist:: python-grammar
817819
subscription: `primary` "[" `expression_list` "]"
818820

819-
The primary must evaluate to an object that supports subscription (lists or
820-
dictionaries for example). User-defined objects can support subscription by
821-
defining a :meth:`__getitem__` method.
821+
When an object is subscripted, the interpreter will evaluate the primary and
822+
the expression list.
822823

823-
For built-in objects, there are two types of objects that support subscription:
824+
The primary must evaluate to an object that supports subscription. An object
825+
may support subscription through defining one or both of
826+
:meth:`~object.__getitem__` and :meth:`~object.__class_getitem__`. When the
827+
primary is subscripted, the evaluated result of the expression list will be
828+
passed to one of these methods. For more details on when ``__class_getitem__``
829+
is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`.
824830

825-
If the primary is a mapping, the expression list must evaluate to an object
826-
whose value is one of the keys of the mapping, and the subscription selects the
827-
value in the mapping that corresponds to that key. (The expression list is a
828-
tuple except if it has exactly one item.)
831+
If the expression list contains at least one comma, it will evaluate to a
832+
:class:`tuple` containing the items of the expression list. Otherwise, the
833+
expression list will evaluate to the value of the list's sole member.
829834

830-
If the primary is a sequence, the expression list must evaluate to an integer
831-
or a slice (as discussed in the following section).
835+
For built-in objects, there are two types of objects that support subscription
836+
via :meth:`~object.__getitem__`:
837+
838+
1. Mappings. If the primary is a :term:`mapping`, the expression list must
839+
evaluate to an object whose value is one of the keys of the mapping, and the
840+
subscription selects the value in the mapping that corresponds to that key.
841+
An example of a builtin mapping class is the :class:`dict` class.
842+
2. Sequences. If the primary is a :term:`sequence`, the expression list must
843+
evaluate to an :class:`int` or a :class:`slice` (as discussed in the
844+
following section). Examples of builtin sequence classes include the
845+
:class:`str`, :class:`list` and :class:`tuple` classes.
832846

833847
The formal syntax makes no special provision for negative indices in
834-
sequences; however, built-in sequences all provide a :meth:`__getitem__`
848+
:term:`sequences <sequence>`. However, built-in sequences all provide a :meth:`~object.__getitem__`
835849
method that interprets negative indices by adding the length of the sequence
836-
to the index (so that ``x[-1]`` selects the last item of ``x``). The
850+
to the index so that, for example, ``x[-1]`` selects the last item of ``x``. The
837851
resulting value must be a nonnegative integer less than the number of items in
838852
the sequence, and the subscription selects the item whose index is that value
839853
(counting from zero). Since the support for negative indices and slicing
@@ -844,14 +858,10 @@ this method will need to explicitly add that support.
844858
single: character
845859
pair: string; item
846860

847-
A string's items are characters. A character is not a separate data type but a
861+
A :class:`string <str>` is a special kind of sequence whose items are
862+
*characters*. A character is not a separate data type but a
848863
string of exactly one character.
849864

850-
Subscription of certain :term:`classes <class>` or :term:`types <type>`
851-
creates a :ref:`generic alias <types-genericalias>`.
852-
In this case, user-defined classes can support subscription by providing a
853-
:meth:`__class_getitem__` classmethod.
854-
855865

856866
.. _slicings:
857867

0 commit comments

Comments
 (0)