@@ -310,6 +310,9 @@ including built-in sequences, interpret negative subscripts by adding the
310310sequence length. For example, ``a[-2] `` equals ``a[n-2] ``, the second to last
311311item 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
387395Bytes
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
0 commit comments