Skip to content

Commit 5073129

Browse files
authored
bpo-45680: Improve docs on subscriptions w.r.t. GenericAlias objects (GH-29479)
1 parent 47cca04 commit 5073129

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
@@ -815,30 +815,44 @@ Subscriptions
815815
object: dictionary
816816
pair: sequence; item
817817

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

821823
.. productionlist:: python-grammar
822824
subscription: `primary` "[" `expression_list` "]"
823825

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

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

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

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

838852
The formal syntax makes no special provision for negative indices in
839-
sequences; however, built-in sequences all provide a :meth:`__getitem__`
853+
:term:`sequences <sequence>`. However, built-in sequences all provide a :meth:`~object.__getitem__`
840854
method that interprets negative indices by adding the length of the sequence
841-
to the index (so that ``x[-1]`` selects the last item of ``x``). The
855+
to the index so that, for example, ``x[-1]`` selects the last item of ``x``. The
842856
resulting value must be a nonnegative integer less than the number of items in
843857
the sequence, and the subscription selects the item whose index is that value
844858
(counting from zero). Since the support for negative indices and slicing
@@ -849,14 +863,10 @@ this method will need to explicitly add that support.
849863
single: character
850864
pair: string; item
851865

852-
A string's items are characters. A character is not a separate data type but a
866+
A :class:`string <str>` is a special kind of sequence whose items are
867+
*characters*. A character is not a separate data type but a
853868
string of exactly one character.
854869

855-
Subscription of certain :term:`classes <class>` or :term:`types <type>`
856-
creates a :ref:`generic alias <types-genericalias>`.
857-
In this case, user-defined classes can support subscription by providing a
858-
:meth:`__class_getitem__` classmethod.
859-
860870

861871
.. _slicings:
862872

0 commit comments

Comments
 (0)