@@ -802,30 +802,44 @@ Subscriptions
802
802
object: dictionary
803
803
pair: sequence; item
804
804
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.
807
809
808
810
.. productionlist :: python-grammar
809
811
subscription: `primary ` "[" `expression_list ` "]"
810
812
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.
814
815
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 `.
816
822
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.
821
826
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.
824
838
825
839
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__ `
827
841
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
829
843
resulting value must be a nonnegative integer less than the number of items in
830
844
the sequence, and the subscription selects the item whose index is that value
831
845
(counting from zero). Since the support for negative indices and slicing
@@ -836,14 +850,10 @@ this method will need to explicitly add that support.
836
850
single: character
837
851
pair: string; item
838
852
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
840
855
string of exactly one character.
841
856
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
-
847
857
848
858
.. _slicings :
849
859
0 commit comments