@@ -815,30 +815,44 @@ Subscriptions
815
815
object: dictionary
816
816
pair: sequence; item
817
817
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.
820
822
821
823
.. productionlist :: python-grammar
822
824
subscription: `primary ` "[" `expression_list ` "]"
823
825
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.
827
828
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 `.
829
835
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.
834
839
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.
837
851
838
852
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__ `
840
854
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
842
856
resulting value must be a nonnegative integer less than the number of items in
843
857
the sequence, and the subscription selects the item whose index is that value
844
858
(counting from zero). Since the support for negative indices and slicing
@@ -849,14 +863,10 @@ this method will need to explicitly add that support.
849
863
single: character
850
864
pair: string; item
851
865
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
853
868
string of exactly one character.
854
869
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
-
860
870
861
871
.. _slicings :
862
872
0 commit comments