@@ -931,7 +931,6 @@ If an :exc:`AttributeError` is raised and the object has a :meth:`!__getattr__`
931931method, that method is called as a fallback.
932932
933933.. _subscriptions :
934- .. _slicings :
935934
936935Subscriptions and slicing
937936-------------------------
@@ -958,42 +957,31 @@ a :class:`dict`::
958957 2
959958
960959In the subscription syntax, the object being subscribed -- a
961- :ref: `primary <primaries >` -- is followed by square brackets.
962- In the simplest case, the brackets contain a single expression.
960+ :ref: `primary <primaries >` -- is followed by a :dfn: `subscript ` in
961+ square brackets.
962+ In the simplest case, the subscript is single expression.
963963
964- When getting a value from a :ref: ` mapping < datamodel-mappings >`, like in the
965- :class: ` ~dict ` example above, the expression in brackets is called a *key *.
966- When when getting an item from a :ref: ` sequence < datamodel-sequences >`,
967- the expression is called an * index * ::
964+ Depending on the type of the object being subscribed, the subscript is
965+ sometimes called a *key * (for mappings), * index * (for sequences),
966+ or * type argument * (for :term: ` generic types <generic type> `).
967+ Syntacticall, these are all equivalent ::
968968
969969 >>> number_names = ['zero', 'one', 'two', 'three', 'four', 'five']
970970 >>> number_names[2] # Subscripting a list using the index 2
971971 'two'
972- >>> number_names[-2]
973- 'four'
974-
975- Syntactically, there's no difference between a *key * and an *index *.
976-
977- The subscription syntax is also used to provide type arguments for
978- :term: `generic types <generic type> `, even though types are not (necessarily)
979- sequences::
980972
981973 >>> list[str] # Parameterizing `list` using the type argument `str`
982974 list[str]
983975
984- Syntactically, there's also no difference between a *type argument * and a
985- key or index.
986-
987976At runtime, the interpreter will evaluate the primary and
988- the expression in the square brackets, and call the primary's
989- :meth: `~object.__getitem__ ` or :meth: `~object.__class_getitem__ ` method
990- with the contents of the square brackets as argument.
977+ the subscript, and call the primary's :meth: `~object.__getitem__ ` or
978+ :meth: `~object.__class_getitem__ ` method with the subscript as argument.
991979For more details on which of these methods is called, see
992980:ref: `classgetitem-versus-getitem `.
993981
994982To show how subscription works, we can define a custom object that
995- implements :meth: `~object.__getitem__ ` and prints out the key it
996- was subscripted with ::
983+ implements :meth: `~object.__getitem__ ` and prints out the value of
984+ the subscript ::
997985
998986 >>> class SubscriptionDemo:
999987 ... def __getitem__(self, key):
@@ -1006,7 +994,7 @@ was subscripted with::
1006994 subscripted with: 'aaa'
1007995
1008996See :meth: `~object.__getitem__ ` documentation for how built-in types handle
1009- subscription, including support for negative indices .
997+ subscription, including support for negative subscripts .
1010998
1011999
10121000.. index ::
@@ -1028,7 +1016,7 @@ Slicing
10281016
10291017A more advanced form of subscription, :dfn: `slicing `, is commonly used
10301018to extract a portion of a :ref: `sequence <datamodel-sequences >`.
1031- In this form, the square brackets contain a :term: `slice `: up to three
1019+ In this form, the subscript is a :term: `slice `: up to three
10321020expressions separated by colons.
10331021Any of the expressions may be omitted, but a slice must contain at least one
10341022colon::
@@ -1062,19 +1050,21 @@ or :meth:`~object.__class_getitem__` method, as above. ::
10621050Tuple subscription
10631051^^^^^^^^^^^^^^^^^^
10641052
1065- The square brackets used for subscription can also contain two or more
1066- comma-separated expressions or slices::
1053+ The subscript can also be given as two or more comma-separated expressions
1054+ or slices::
10671055
10681056 >>> demo[1, 2, 3]
10691057 subscripted with (1, 2, 3)
1058+ >>> demo[1:2, 3]
1059+ subscripted with (slice(1, 2, None), 3)
10701060
10711061This form is commonly used with numerical libraries for slicing
10721062multi-dimensional data.
10731063In this case, the interpreter constructs a :class: `tuple ` of the results of the
10741064expressions or slices, and passes this tuple to the :meth: `~object.__getitem__ `
10751065or :meth: `~object.__class_getitem__ ` method, as above.
10761066
1077- The square brackets may also contain one expression or slice followed
1067+ The subscript may also be given as a single expression or slice followed
10781068by a comma, to specify a one-element tuple::
10791069
10801070 >>> demo['spam',]
@@ -1087,7 +1077,7 @@ by a comma, to specify a one-element tuple::
10871077.. versionadded :: 3.11
10881078 Expressions in *tuple_slices * may be starred. See :pep: `646 `.
10891079
1090- The square brackets can also contain a starred expression.
1080+ The subscript can also contain a starred expression.
10911081In this case, the interpreter unpacks the result into a tuple, and passes
10921082this tuple to :meth: `~object.__getitem__ ` or :meth: `~object.__class_getitem__ `::
10931083
@@ -1107,19 +1097,19 @@ Formal subscription grammar
11071097.. grammar-snippet ::
11081098 :group: python-grammar
11091099
1110- subscription: `primary ` '[' `slices ` ']'
1111- slices: `slice ` | `tuple_slices `
1100+ subscription: `primary ` '[' `subscript ` ']'
1101+ subscript: `slice ` | `tuple_slices `
11121102 tuple_slices: ','.(`slice ` | `starred_expression `)+ [',']
11131103 slice: `proper_slice ` | `assignment_expression `
11141104 proper_slice: [`lower_bound `] ":" [`upper_bound `] [ ":" [`stride `] ]
11151105 lower_bound: `expression `
11161106 upper_bound: `expression `
11171107 stride: `expression `
11181108
1119- If *slices * contains ony one unstarred *slice * without a trailing comma,
1109+ If *subscript * contains ony one unstarred *slice * without a trailing comma,
11201110it will evaluate to the value of that *slice *.
1121- Otherwise, *slices * will evaluate to a :class: `tuple ` containing
1122- the items of *slices *.
1111+ Otherwise, *subscript * will evaluate to a :class: `tuple ` containing
1112+ the items of *subscript *.
11231113
11241114.. index ::
11251115 pair: object; callable
0 commit comments