Skip to content

Commit c8d0f9e

Browse files
committed
Continue rewording
1 parent 0cd76e6 commit c8d0f9e

File tree

3 files changed

+59
-63
lines changed

3 files changed

+59
-63
lines changed

Doc/library/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,8 +1822,8 @@ are always available. They are listed here in alphabetical order.
18221822
.. attribute:: slice.stop
18231823
.. attribute:: slice.step
18241824

1825-
Slice objects are also generated when extended indexing syntax is used. For
1826-
example: ``a[start:stop:step]`` or ``a[start:stop, i]``. See
1825+
Slice objects are also generated when :ref:`slicing syntax <slicings>`
1826+
is used. For example: ``a[start:stop:step]`` or ``a[start:stop, i]``. See
18271827
:func:`itertools.islice` for an alternate version that returns an
18281828
:term:`iterator`.
18291829

Doc/reference/datamodel.rst

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,22 @@ including built-in sequences, interpret negative subscripts by adding the
314314
sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
315315
item of sequence a with length ``n``.
316316

317-
.. index:: single: slicing
317+
.. index::
318+
single: slicing
319+
single: start (slice object attribute)
320+
single: stop (slice object attribute)
321+
single: step (slice object attribute)
318322

319-
Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
320-
that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
321-
sequence of the same type. The comment above about negative indexes also applies
323+
Sequences also support slicing: ``a[start:stop]`` selects all items with index *k* such
324+
that *start* ``<=`` *k* ``<`` *stop*. When used as an expression, a slice is a
325+
sequence of the same type. The comment above about negative subscripts also applies
322326
to negative slice positions.
327+
Note that no error is raised if a slice positions is larger than the length
328+
of the sequence.
329+
330+
If *start* is missing or ``None``, slicing behaves as if *start* was zero.
331+
If *stop* is missing or ``None``, slicing behaves as if *stop* was equal to
332+
the length of the sequence.
323333

324334
Some sequences also support "extended slicing" with a third "step" parameter:
325335
``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*
@@ -3203,21 +3213,28 @@ through the object's keys; for sequences, it should iterate through the values.
32033213
and so forth. Missing slice items are always filled in with ``None``.
32043214

32053215

3206-
.. method:: object.__getitem__(self, key)
3216+
.. method:: object.__getitem__(self, subscript)
32073217

3208-
<<<<
3218+
Called to implement *subscription*, that is, ``self[subscript]``.
3219+
See :ref:`subscriptions` for details on the syntax.
32093220

3210-
For built-in objects, there are two types of objects that support subscription
3211-
via :meth:`~object.__getitem__`:
3221+
There are two types of built-in objects that support subscription
3222+
via :meth:`~object.__getitem__`:
3223+
- **sequences**, where *subscript* (also called
3224+
*index*) should be an integer or a :class:`slice` object.
3225+
See :ref:`sequence documentation <datamodel-sequences>` for the expected
3226+
behavior, including handling :class:`slice` objects and negative indices.
3227+
- **mappings**, where *subscript* is also called the *key*.
3228+
See :ref:`mapping documentation <datamodel-mappings>` for the expected
3229+
behavior.
32123230

3213-
1. Mappings. If the primary is a :term:`mapping`, the expression list must
3214-
evaluate to an object whose value is one of the keys of the mapping, and the
3215-
subscription selects the value in the mapping that corresponds to that key.
3216-
An example of a builtin mapping class is the :class:`dict` class.
3217-
2. Sequences. If the primary is a :term:`sequence`, the expression list must
3218-
evaluate to an :class:`int` or a :class:`slice` (as discussed in the
3219-
following section). Examples of builtin sequence classes include the
3220-
:class:`str`, :class:`list` and :class:`tuple` classes.
3231+
If *subscript* is of an inappropriate type, :meth:`~object.__getitem__`
3232+
should raise :exc:`TypeError`.
3233+
If *subscript* has an inappropriate value, :meth:`~object.__getitem__`
3234+
should raise an :exc:`LookupError` or one of its subclasses
3235+
(:exc:`IndexError` for sequences; :exc:`KeyError` for mappings).
3236+
3237+
<<<<
32213238

32223239
The formal syntax makes no special provision for negative indices in
32233240
:term:`sequences <sequence>`. However, built-in sequences all provide a :meth:`~object.__getitem__`
@@ -3263,17 +3280,6 @@ expressions given as lower bound, upper bound and stride, respectively,
32633280
substituting ``None`` for missing expressions.
32643281

32653282

3266-
====
3267-
3268-
Called to implement evaluation of ``self[key]``. For :term:`sequence` types,
3269-
the accepted keys should be integers. Optionally, they may support
3270-
:class:`slice` objects as well. Negative index support is also optional.
3271-
If *key* is
3272-
of an inappropriate type, :exc:`TypeError` may be raised; if *key* is a value
3273-
outside the set of indexes for the sequence (after any special
3274-
interpretation of negative values), :exc:`IndexError` should be raised. For
3275-
:term:`mapping` types, if *key* is missing (not in the container),
3276-
:exc:`KeyError` should be raised.
32773283

32783284
>>>>>>
32793285

Doc/reference/expressions.rst

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,6 @@ If an :exc:`AttributeError` is raised and the object has a :meth:`!__getattr__`
931931
method, that method is called as a fallback.
932932

933933
.. _subscriptions:
934-
.. _slicings:
935934

936935
Subscriptions and slicing
937936
-------------------------
@@ -958,42 +957,31 @@ a :class:`dict`::
958957
2
959958

960959
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.
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-
987976
At 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.
991979
For more details on which of these methods is called, see
992980
:ref:`classgetitem-versus-getitem`.
993981

994982
To 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

1008996
See :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

10291017
A more advanced form of subscription, :dfn:`slicing`, is commonly used
10301018
to 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
10321020
expressions separated by colons.
10331021
Any of the expressions may be omitted, but a slice must contain at least one
10341022
colon::
@@ -1062,19 +1050,21 @@ or :meth:`~object.__class_getitem__` method, as above. ::
10621050
Tuple 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

10711061
This form is commonly used with numerical libraries for slicing
10721062
multi-dimensional data.
10731063
In this case, the interpreter constructs a :class:`tuple` of the results of the
10741064
expressions or slices, and passes this tuple to the :meth:`~object.__getitem__`
10751065
or :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
10781068
by 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.
10911081
In this case, the interpreter unpacks the result into a tuple, and passes
10921082
this 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,
11201110
it 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

Comments
 (0)