Skip to content

Commit f5675ae

Browse files
miss-islingtonAlexWaygood
authored andcommitted
bpo-45680: Improve docs on subscriptions w.r.t. GenericAlias objects (pythonGH-29479)
(cherry picked from commit 5073129) Co-authored-by: Alex Waygood <[email protected]>
1 parent deb6ef9 commit f5675ae

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
@@ -802,30 +802,44 @@ Subscriptions
802802
object: dictionary
803803
pair: sequence; item
804804

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

808810
.. productionlist:: python-grammar
809811
subscription: `primary` "[" `expression_list` "]"
810812

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

815-
For built-in objects, there are two types of objects that support subscription:
816+
The primary must evaluate to an object that supports subscription. An object
817+
may support subscription through defining one or both of
818+
:meth:`~object.__getitem__` and :meth:`~object.__class_getitem__`. When the
819+
primary is subscripted, the evaluated result of the expression list will be
820+
passed to one of these methods. For more details on when ``__class_getitem__``
821+
is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`.
816822

817-
If the primary is a mapping, the expression list must evaluate to an object
818-
whose value is one of the keys of the mapping, and the subscription selects the
819-
value in the mapping that corresponds to that key. (The expression list is a
820-
tuple except if it has exactly one item.)
823+
If the expression list contains at least one comma, it will evaluate to a
824+
:class:`tuple` containing the items of the expression list. Otherwise, the
825+
expression list will evaluate to the value of the list's sole member.
821826

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

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

839-
A string's items are characters. A character is not a separate data type but a
853+
A :class:`string <str>` is a special kind of sequence whose items are
854+
*characters*. A character is not a separate data type but a
840855
string of exactly one character.
841856

842-
Subscription of certain :term:`classes <class>` or :term:`types <type>`
843-
creates a :ref:`generic alias <types-genericalias>`.
844-
In this case, user-defined classes can support subscription by providing a
845-
:meth:`__class_getitem__` classmethod.
846-
847857

848858
.. _slicings:
849859

0 commit comments

Comments
 (0)