@@ -810,30 +810,44 @@ Subscriptions
810
810
object: dictionary
811
811
pair: sequence; item
812
812
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.
815
817
816
818
.. productionlist :: python-grammar
817
819
subscription: `primary ` "[" `expression_list ` "]"
818
820
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.
822
823
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 `.
824
830
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.
829
834
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.
832
846
833
847
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__ `
835
849
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
837
851
resulting value must be a nonnegative integer less than the number of items in
838
852
the sequence, and the subscription selects the item whose index is that value
839
853
(counting from zero). Since the support for negative indices and slicing
@@ -844,14 +858,10 @@ this method will need to explicitly add that support.
844
858
single: character
845
859
pair: string; item
846
860
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
848
863
string of exactly one character.
849
864
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
-
855
865
856
866
.. _slicings :
857
867
0 commit comments