Skip to content

Commit e755b5d

Browse files
encukoublaisep
andcommitted
More work here
Co-authored-by: Blaise Pabon <blaise@gmail.com>
1 parent d3e1c13 commit e755b5d

File tree

3 files changed

+64
-83
lines changed

3 files changed

+64
-83
lines changed

Doc/library/functions.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,19 +1822,19 @@ are always available. They are listed here in alphabetical order.
18221822
``range(start, stop, step)``. The *start* and *step* arguments default to
18231823
``None``.
18241824

1825-
Slice objects have read-only data attributes :attr:`!start`,
1826-
:attr:`!stop`, and :attr:`!step` which merely return the argument
1827-
values (or their default). They have no other explicit functionality;
1828-
however, they are used by NumPy and other third-party packages.
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]``.
1827+
1828+
See :func:`itertools.islice` for an alternate version that returns an
1829+
:term:`iterator`.
18291830

18301831
.. attribute:: slice.start
1831-
.. attribute:: slice.stop
1832-
.. attribute:: slice.step
1832+
slice.stop
1833+
slice.step
18331834

1834-
Slice objects are also generated when :ref:`slicing syntax <slicings>`
1835-
is used. For example: ``a[start:stop:step]`` or ``a[start:stop, i]``. See
1836-
:func:`itertools.islice` for an alternate version that returns an
1837-
:term:`iterator`.
1835+
These read-only attributes are set to the argument values
1836+
(or their default). They have no other explicit functionality;
1837+
however, they are used by NumPy and other third-party packages.
18381838

18391839
.. versionchanged:: 3.12
18401840
Slice objects are now :term:`hashable` (provided :attr:`~slice.start`,

Doc/reference/datamodel.rst

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ including built-in sequences, interpret negative subscripts by adding the
310310
sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
311311
item of sequence a with length ``n``.
312312

313+
The resulting value must be a nonnegative integer less than the number of items
314+
in the sequence. If it is not, an :exc:`IndexError` is raised.
315+
313316
.. index::
314317
single: slicing
315318
single: start (slice object attribute)
@@ -356,17 +359,22 @@ Strings
356359
pair: built-in function; chr
357360
pair: built-in function; ord
358361
single: character
359-
single: integer
362+
pair: string; item
360363
single: Unicode
361364

362-
A string is a sequence of values that represent Unicode code points.
363-
All the code points in the range ``U+0000 - U+10FFFF`` can be
364-
represented in a string. Python doesn't have a :c:expr:`char` type;
365-
instead, every code point in the string is represented as a string
366-
object with length ``1``. The built-in function :func:`ord`
365+
A string (:class:`str`) is a sequence of values that represent
366+
:dfn:`characters`, or more formally, *Unicode code points*.
367+
All the code points in the range ``0`` to ``0x10FFFF`` can be
368+
represented in a string.
369+
370+
Python doesn't have a dedicated *character* type.
371+
Instead, every code point in the string is represented as a string
372+
object with length ``1``.
373+
374+
The built-in function :func:`ord`
367375
converts a code point from its string form to an integer in the
368-
range ``0 - 10FFFF``; :func:`chr` converts an integer in the range
369-
``0 - 10FFFF`` to the corresponding length ``1`` string object.
376+
range ``0`` to ``0x10FFFF``; :func:`chr` converts an integer in the range
377+
``0`` to ``0x10FFFF`` to the corresponding length ``1`` string object.
370378
:meth:`str.encode` can be used to convert a :class:`str` to
371379
:class:`bytes` using the given text encoding, and
372380
:meth:`bytes.decode` can be used to achieve the opposite.
@@ -377,7 +385,7 @@ Tuples
377385
pair: singleton; tuple
378386
pair: empty; tuple
379387

380-
The items of a tuple are arbitrary Python objects. Tuples of two or
388+
The items of a :class:`tuple` are arbitrary Python objects. Tuples of two or
381389
more items are formed by comma-separated lists of expressions. A tuple
382390
of one item (a 'singleton') can be formed by affixing a comma to an
383391
expression (an expression by itself does not create a tuple, since
@@ -387,7 +395,7 @@ Tuples
387395
Bytes
388396
.. index:: bytes, byte
389397

390-
A bytes object is an immutable array. The items are 8-bit bytes,
398+
A :class:`bytes` object is an immutable array. The items are 8-bit bytes,
391399
represented by integers in the range 0 <= x < 256. Bytes literals
392400
(like ``b'abc'``) and the built-in :func:`bytes` constructor
393401
can be used to create bytes objects. Also, bytes objects can be
@@ -3238,7 +3246,8 @@ through the object's keys; for sequences, it should iterate through the values.
32383246
See :ref:`subscriptions` for details on the syntax.
32393247

32403248
There are two types of built-in objects that support subscription
3241-
via :meth:`~object.__getitem__`:
3249+
via :meth:`!__getitem__`:
3250+
32423251
- **sequences**, where *subscript* (also called
32433252
*index*) should be an integer or a :class:`slice` object.
32443253
See :ref:`sequence documentation <datamodel-sequences>` for the expected
@@ -3247,71 +3256,23 @@ through the object's keys; for sequences, it should iterate through the values.
32473256
See :ref:`mapping documentation <datamodel-mappings>` for the expected
32483257
behavior.
32493258

3250-
If *subscript* is of an inappropriate type, :meth:`~object.__getitem__`
3259+
If *subscript* is of an inappropriate type, :meth:`!__getitem__`
32513260
should raise :exc:`TypeError`.
3252-
If *subscript* has an inappropriate value, :meth:`~object.__getitem__`
3261+
If *subscript* has an inappropriate value, :meth:`!__getitem__`
32533262
should raise an :exc:`LookupError` or one of its subclasses
32543263
(:exc:`IndexError` for sequences; :exc:`KeyError` for mappings).
32553264

3256-
<<<<
3257-
3258-
The formal syntax makes no special provision for negative indices in
3259-
:term:`sequences <sequence>`. However, built-in sequences all provide a :meth:`~object.__getitem__`
3260-
method that interprets negative indices by adding the length of the sequence
3261-
to the index so that, for example, ``x[-1]`` selects the last item of ``x``. The
3262-
resulting value must be a nonnegative integer less than the number of items in
3263-
the sequence, and the subscription selects the item whose index is that value
3264-
(counting from zero). Since the support for negative indices and slicing
3265-
occurs in the object's :meth:`~object.__getitem__` method, subclasses overriding
3266-
this method will need to explicitly add that support.
3267-
3268-
.. index::
3269-
single: character
3270-
pair: string; item
3271-
3272-
A :class:`string <str>` is a special kind of sequence whose items are
3273-
*characters*. A character is not a separate data type but a
3274-
string of exactly one character.
3275-
3276-
...
3277-
3278-
3279-
A slicing selects a range of items in a sequence object (e.g., a string, tuple
3280-
or list). Slicings may be used as expressions or as targets in assignment or
3281-
:keyword:`del` statements. The syntax for a slicing:
3282-
3283-
3284-
.. index::
3285-
single: start (slice object attribute)
3286-
single: stop (slice object attribute)
3287-
single: step (slice object attribute)
3288-
3289-
The semantics for a slicing are as follows. The primary is indexed (using the
3290-
same :meth:`~object.__getitem__` method as
3291-
normal subscription) with a key that is constructed from the slice list, as
3292-
follows. If the slice list contains at least one comma, the key is a tuple
3293-
containing the conversion of the slice items; otherwise, the conversion of the
3294-
lone slice item is the key. The conversion of a slice item that is an
3295-
expression is that expression. The conversion of a proper slice is a slice
3296-
object (see section :ref:`types`) whose :attr:`~slice.start`,
3297-
:attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the
3298-
expressions given as lower bound, upper bound and stride, respectively,
3299-
substituting ``None`` for missing expressions.
3300-
3301-
3302-
3303-
>>>>>>
3304-
33053265
.. note::
33063266

3307-
:keyword:`for` loops expect that an :exc:`IndexError` will be raised for
3308-
illegal indexes to allow proper detection of the end of the sequence.
3267+
The sequence iteration protocol (used, for example, in :keyword:`for`
3268+
loops), expects that an :exc:`IndexError` will be raised for illegal
3269+
indexes to allow proper detection of the end of a sequence.
33093270

33103271
.. note::
33113272

3312-
When :ref:`subscripting<subscriptions>` a *class*, the special
3273+
When :ref:`subscripting <subscriptions>` a *class*, the special
33133274
class method :meth:`~object.__class_getitem__` may be called instead of
3314-
``__getitem__()``. See :ref:`classgetitem-versus-getitem` for more
3275+
:meth:`!__getitem__`. See :ref:`classgetitem-versus-getitem` for more
33153276
details.
33163277

33173278

Doc/reference/expressions.rst

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -964,13 +964,13 @@ In the simplest case, the subscript is single expression.
964964
Depending on the type of the object being subscribed, the subscript is
965965
sometimes called a *key* (for mappings), *index* (for sequences),
966966
or *type argument* (for :term:`generic types <generic type>`).
967-
Syntacticall, these are all equivalent::
967+
Syntactically, these are all equivalent::
968968

969-
>>> number_names = ['zero', 'one', 'two', 'three', 'four', 'five']
970-
>>> number_names[2] # Subscripting a list using the index 2
971-
'two'
969+
>>> colors = ['red', 'blue', 'green', 'black']
970+
>>> colors[3] # Subscripting a list using the index 3
971+
'black'
972972

973-
>>> list[str] # Parameterizing `list` using the type argument `str`
973+
>>> list[str] # Parameterizing the list type using the type argument str
974974
list[str]
975975

976976
At runtime, the interpreter will evaluate the primary and
@@ -994,7 +994,26 @@ the subscript::
994994
subscripted with: 'aaa'
995995

996996
See :meth:`~object.__getitem__` documentation for how built-in types handle
997-
subscription, including support for negative subscripts.
997+
subscription.
998+
999+
Subscriptions may also be used as targets in :ref:`assignment <assignment>` or
1000+
:ref:`deletion <del>` statements.
1001+
In these cases, the interpreter will call the subscripted object's
1002+
:meth:`~object.__setitem__` or :meth:`~object.__delitem__` method,
1003+
respectively, instead of :meth:`~object.__getitem__`.
1004+
1005+
.. code-block::
1006+
1007+
>>> colors = ['red', 'blue', 'green', 'black']
1008+
>>> colors[3] = 'white' # Setting item at index
1009+
>>> colors
1010+
['red', 'blue', 'green', 'white']
1011+
>>> del colors[3] # Deleting item at index 3
1012+
>>> colors
1013+
['red', 'blue', 'green']
1014+
1015+
All advanced forms of *subscript* documented in the following sections
1016+
are also usable for assignment and deletion.
9981017

9991018

10001019
.. index::
@@ -1106,10 +1125,11 @@ Formal subscription grammar
11061125
upper_bound: `expression`
11071126
stride: `expression`
11081127

1128+
There is a semantic difference between the alternatives for *subscript*.
11091129
If *subscript* contains ony one unstarred *slice* without a trailing comma,
11101130
it will evaluate to the value of that *slice*.
11111131
Otherwise, *subscript* will evaluate to a :class:`tuple` containing
1112-
the items of *subscript*.
1132+
the items of *tuple_slices*.
11131133

11141134
.. index::
11151135
pair: object; callable

0 commit comments

Comments
 (0)