@@ -949,34 +949,45 @@ Subscriptions and slicing
949949 pair: object; dictionary
950950 pair: sequence; item
951951
952- :dfn: `Subscription ` is an operation of selecting an element from a
953- :ref: `container <sequence-types >`.
954- The subscription syntax uses square brackets, which contain a :dfn: `key `,
955- :dfn: `index ` or :dfn: `slice ` -- that is, an expression by which the requested
956- element is looked up.
957- For example, subscription is used to get a value from a
958- :ref: `mapping <datamodel-mappings >` using a key, or to get an item from a
959- :ref: `sequence <datamodel-sequences >` using an index::
952+ The :dfn: `subscription ` syntax is usually used for selecting an element from a
953+ :ref: `container <sequence-types >` -- for example, to get a value from
954+ a :class: `dict `::
960955
961956 >>> digits_by_name = {'one': 1, 'two': 2}
962- >>> digits_by_name['two']
957+ >>> digits_by_name['two'] # Subscripting a dictionary using the key 'two'
963958 2
964959
960+ In 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.
963+
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 *::
968+
965969 >>> number_names = ['zero', 'one', 'two', 'three', 'four', 'five']
966- >>> number_names[2]
970+ >>> number_names[2] # Subscripting a list using the index 2
967971 'two'
968972 >>> number_names[-2]
969973 'four'
970974
975+ Syntactically, there's no difference between a *key * and an *index *.
976+
971977The subscription syntax is also used to provide type arguments for
972- :term: `generic types <generic type> `::
978+ :term: `generic types <generic type> `, even though types are not (necessarily)
979+ sequences::
973980
974- >>> seq: list[str]
981+ >>> list[str] # Parameterizing `list` using the type argument `str`
982+ list[str]
983+
984+ Syntactically, there's also no difference between a *type argument * and a
985+ key or index.
975986
976- Syntactically, the object being subscribed is a :ref: `primary <primaries >`.
977987At runtime, the interpreter will evaluate the primary and
978- the contents of the square brackets, and call the primary's
979- :meth: `~object.__getitem__ ` or :meth: `~object.__class_getitem__ ` method.
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.
980991For more details on which of these methods is called, see
981992:ref: `classgetitem-versus-getitem `.
982993
@@ -991,14 +1002,29 @@ was subscripted with::
9911002 >>> demo = SubscriptionDemo()
9921003 >>> demo[1]
9931004 subscripted with: 1
1005+ >>> demo['a' * 3]
1006+ subscripted with: 'aaa'
9941007
995- In the simplest form of subscription, the square brackets contain a single
996- expression.
997- The evaluated result of this expression will be passed to the
998- :meth: `~object.__getitem__ ` or :meth: `~object.__class_getitem__ ` method::
1008+ See :meth: `~object.__getitem__ ` documentation for how built-in types handle
1009+ subscription, including support for negative indices.
9991010
1000- >>> demo[1 + 2]
1001- subscripted with: 3
1011+
1012+ .. index ::
1013+ single: slicing
1014+ single: slice
1015+ single: : (colon); slicing
1016+ single: , (comma); slicing
1017+
1018+ .. index ::
1019+ pair: object; sequence
1020+ pair: object; string
1021+ pair: object; tuple
1022+ pair: object; list
1023+
1024+ .. _slicings :
1025+
1026+ Slicing
1027+ ^^^^^^^
10021028
10031029A more advanced form of subscription, :dfn: `slicing `, is commonly used
10041030to extract a portion of a :ref: `sequence <datamodel-sequences >`.
@@ -1032,20 +1058,35 @@ or :meth:`~object.__class_getitem__` method, as above. ::
10321058 >>> demo[::'spam']
10331059 subscripted with: slice(None, None, 'spam')
10341060
1061+
1062+ Tuple subscription
1063+ ^^^^^^^^^^^^^^^^^^
1064+
10351065The square brackets used for subscription can also contain two or more
1036- comma-separated expressions or slices, or one expression or slice followed
1037- by a comma.
1066+ comma-separated expressions or slices::
1067+
1068+ >>> demo[1, 2, 3]
1069+ subscripted with (1, 2, 3)
1070+
10381071This form is commonly used with numerical libraries for slicing
10391072multi-dimensional data.
10401073In this case, the interpreter constructs a :class: `tuple ` of the results of the
10411074expressions or slices, and passes this tuple to the :meth: `~object.__getitem__ `
1042- or :meth: `~object.__class_getitem__ ` method, as above::
1075+ or :meth: `~object.__class_getitem__ ` method, as above.
1076+
1077+ The square brackets may also contain one expression or slice followed
1078+ by a comma, to specify a one-element tuple::
10431079
1044- >>> demo[1, 2, 3]
1045- subscripted with (1, 2, 3)
10461080 >>> demo['spam',]
10471081 subscripted with ('spam',)
10481082
1083+
1084+ "Starred" subscription
1085+ ^^^^^^^^^^^^^^^^^^^^^^
1086+
1087+ .. versionadded :: 3.11
1088+ Expressions in *tuple_slices * may be starred. See :pep: `646 `.
1089+
10491090The square brackets can also contain a starred expression.
10501091In this case, the interpreter unpacks the result into a tuple, and passes
10511092this tuple to :meth: `~object.__getitem__ ` or :meth: `~object.__class_getitem__ `::
@@ -1060,57 +1101,25 @@ and slices:::
10601101 subscripted with: ('a', 'b', 0, 1, 2, 'c')
10611102
10621103
1104+ Formal subscription grammar
1105+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
10631106
10641107.. grammar-snippet ::
10651108 :group: python-grammar
10661109
10671110 subscription: `primary ` '[' `slices ` ']'
10681111 slices: `slice ` | `tuple_slices `
1069- tuple_slices: ','.(slice | starred_expression)+ [',']
1070-
1071- slice:
1072- | proper_slice
1073- | assignment_expression
1074-
1112+ tuple_slices: ','.(` slice ` | ` starred_expression ` )+ [',']
1113+ slice: ` proper_slice ` | ` assignment_expression `
1114+ proper_slice: [` lower_bound `] ":" [` upper_bound `] [ ":" [` stride `] ]
1115+ lower_bound: ` expression `
1116+ upper_bound: ` expression `
1117+ stride: ` expression `
10751118
10761119If *slices * contains ony one unstarred *slice * without a trailing comma,
10771120it will evaluate to the value of that *slice *.
10781121Otherwise, *slices * will evaluate to a :class: `tuple ` containing
1079- the items of *slices *
1080-
1081- .. grammar-snippet ::
1082- :group: python-grammar
1083-
1084- proper_slice: [`lower_bound `] ":" [`upper_bound `] [ ":" [`stride `] ]
1085- lower_bound: `expression `
1086- upper_bound: `expression `
1087- stride: `expression `
1088-
1089- .. versionchanged :: 3.11
1090- Expressions in *tuple_slices * may be starred. See :pep: `646 `.
1091-
1092- See :meth: `~object.__getitem__ ` documentation for how built-in types handle
1093- subscription, including support for negative indices.
1094-
1095- <<<
1096- .. _slicings :
1097-
1098- Slicings
1099- --------
1100-
1101- .. index ::
1102- single: slicing
1103- single: slice
1104- single: : (colon); slicing
1105- single: , (comma); slicing
1106-
1107- .. index ::
1108- pair: object; sequence
1109- pair: object; string
1110- pair: object; tuple
1111- pair: object; list
1112-
1113- >>>
1122+ the items of *slices *.
11141123
11151124.. index ::
11161125 pair: object; callable
0 commit comments