@@ -290,6 +290,7 @@ floating-point numbers. The same caveats apply as for floating-point numbers.
290290The real and imaginary parts of a complex number ``z`` can be retrieved through
291291the read-only attributes ``z.real`` and ``z.imag``.
292292
293+ .. _datamodel-sequences:
293294
294295Sequences
295296---------
@@ -309,12 +310,25 @@ including built-in sequences, interpret negative subscripts by adding the
309310sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
310311item of sequence a with length ``n``.
311312
312- .. index:: single: slicing
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.
313315
314- Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
315- that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
316- sequence of the same type. The comment above about negative indexes also applies
316+ .. index::
317+ single: slicing
318+ single: start (slice object attribute)
319+ single: stop (slice object attribute)
320+ single: step (slice object attribute)
321+
322+ Sequences also support slicing: ``a[start:stop]`` selects all items with index *k* such
323+ that *start* ``<=`` *k* ``<`` *stop*. When used as an expression, a slice is a
324+ sequence of the same type. The comment above about negative subscripts also applies
317325to negative slice positions.
326+ Note that no error is raised if a slice position is less than zero or larger
327+ than the length of the sequence.
328+
329+ If *start* is missing or :data:`None`, slicing behaves as if *start* was zero.
330+ If *stop* is missing or ``None``, slicing behaves as if *stop* was equal to
331+ the length of the sequence.
318332
319333Some sequences also support "extended slicing" with a third "step" parameter:
320334``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*
@@ -345,17 +359,22 @@ Strings
345359 pair: built-in function; chr
346360 pair: built-in function; ord
347361 single: character
348- single: integer
362+ pair: string; item
349363 single: Unicode
350364
351- A string is a sequence of values that represent Unicode code points.
352- All the code points in the range ``U+0000 - U+10FFFF`` can be
353- represented in a string. Python doesn't have a :c:expr:`char` type;
354- instead, every code point in the string is represented as a string
355- 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`
356375 converts a code point from its string form to an integer in the
357- range ``0 - 10FFFF ``; :func:`chr` converts an integer in the range
358- ``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.
359378 :meth:`str.encode` can be used to convert a :class:`str` to
360379 :class:`bytes` using the given text encoding, and
361380 :meth:`bytes.decode` can be used to achieve the opposite.
@@ -366,7 +385,7 @@ Tuples
366385 pair: singleton; tuple
367386 pair: empty; tuple
368387
369- 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
370389 more items are formed by comma-separated lists of expressions. A tuple
371390 of one item (a 'singleton') can be formed by affixing a comma to an
372391 expression (an expression by itself does not create a tuple, since
@@ -376,7 +395,7 @@ Tuples
376395Bytes
377396 .. index:: bytes, byte
378397
379- 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,
380399 represented by integers in the range 0 <= x < 256. Bytes literals
381400 (like ``b'abc'``) and the built-in :func:`bytes` constructor
382401 can be used to create bytes objects. Also, bytes objects can be
@@ -461,6 +480,8 @@ Frozen sets
461480 a dictionary key.
462481
463482
483+ .. _datamodel-mappings:
484+
464485Mappings
465486--------
466487
@@ -3220,28 +3241,39 @@ through the object's keys; for sequences, it should iterate through the values.
32203241 and so forth. Missing slice items are always filled in with ``None``.
32213242
32223243
3223- .. method:: object.__getitem__(self, key)
3244+ .. method:: object.__getitem__(self, subscript)
3245+
3246+ Called to implement *subscription*, that is, ``self[subscript]``.
3247+ See :ref:`subscriptions` for details on the syntax.
3248+
3249+ There are two types of built-in objects that support subscription
3250+ via :meth:`!__getitem__`:
3251+
3252+ - **sequences**, where *subscript* (also called
3253+ :term:`index`) should be an integer or a :class:`slice` object.
3254+ See the :ref:`sequence documentation <datamodel-sequences>` for the expected
3255+ behavior, including handling :class:`slice` objects and negative indices.
3256+ - **mappings**, where *subscript* is also called the :term:`key`.
3257+ See :ref:`mapping documentation <datamodel-mappings>` for the expected
3258+ behavior.
32243259
3225- Called to implement evaluation of ``self[key]``. For :term:`sequence` types,
3226- the accepted keys should be integers. Optionally, they may support
3227- :class:`slice` objects as well. Negative index support is also optional.
3228- If *key* is
3229- of an inappropriate type, :exc:`TypeError` may be raised; if *key* is a value
3230- outside the set of indexes for the sequence (after any special
3231- interpretation of negative values), :exc:`IndexError` should be raised. For
3232- :term:`mapping` types, if *key* is missing (not in the container),
3233- :exc:`KeyError` should be raised.
3260+ If *subscript* is of an inappropriate type, :meth:`!__getitem__`
3261+ should raise :exc:`TypeError`.
3262+ If *subscript* has an inappropriate value, :meth:`!__getitem__`
3263+ should raise an :exc:`LookupError` or one of its subclasses
3264+ (:exc:`IndexError` for sequences; :exc:`KeyError` for mappings).
32343265
32353266 .. note::
32363267
3237- :keyword:`for` loops expect that an :exc:`IndexError` will be raised for
3238- illegal indexes to allow proper detection of the end of the sequence.
3268+ The sequence iteration protocol (used, for example, in :keyword:`for`
3269+ loops), expects that an :exc:`IndexError` will be raised for illegal
3270+ indexes to allow proper detection of the end of a sequence.
32393271
32403272 .. note::
32413273
3242- When :ref:`subscripting<subscriptions>` a *class*, the special
3274+ When :ref:`subscripting <subscriptions>` a *class*, the special
32433275 class method :meth:`~object.__class_getitem__` may be called instead of
3244- `` __getitem__()` `. See :ref:`classgetitem-versus-getitem` for more
3276+ :meth:`! __getitem__`. See :ref:`classgetitem-versus-getitem` for more
32453277 details.
32463278
32473279
0 commit comments