Skip to content
33 changes: 29 additions & 4 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,19 @@ Glossary
An object that both finds and loads a module; both a
:term:`finder` and :term:`loader` object.

index
A numeric value that represents the position of an element in
a :term:`sequence`.

In Python, indexing starts at zero.
For example, ``things[0]`` names the *first* element of ``things``;
``things[1]`` names the second one.

In some contexts, Python allows negative indexes for counting from the
end of a sequence, and indexing using :term:`slices <slice>`.

See also :term:`subscript`.

interactive
Python has an interactive interpreter which means you can enter
statements and expressions at the interpreter prompt, immediately
Expand Down Expand Up @@ -799,6 +812,9 @@ Glossary
And also please note that the free-threading CPython does not guarantee
the thread-safety of iterator operations.

key
A value that identifies an entry in a :term:`mapping`.
See also :term:`subscript`.

key function
A key function or collation function is a callable that returns a value
Expand Down Expand Up @@ -1279,10 +1295,11 @@ Glossary
chosen based on the type of a single argument.

slice
An object usually containing a portion of a :term:`sequence`. A slice is
created using the subscript notation, ``[]`` with colons between numbers
when several are given, such as in ``variable_name[1:3:5]``. The bracket
(subscript) notation uses :class:`slice` objects internally.
An object of type :class:`slice`, used to describe a portion of
a :term:`sequence`.
A slice object is created when using the :ref:`slicing <slicings>` form
of :ref:`subscript notation <subscriptions>`, with colons inside square
brackets, such as in ``variable_name[1:3:5]``.

soft deprecated
A soft deprecated API should not be used in new code,
Expand Down Expand Up @@ -1340,6 +1357,14 @@ Glossary

See also :term:`borrowed reference`.

subscript
The expression in square brackets of a
:ref:`subscription expression <subscriptions>`, for example,
the ``3`` in ``items[3]``.
Usually used to select an element of a container.
Also called a :term:`key` when subscripting a :term:`mapping`,
or :term:`index` when subscripting a :term:`sequence`.

t-string
t-strings
String literals prefixed with ``t`` or ``T`` are commonly called
Expand Down
20 changes: 10 additions & 10 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1822,19 +1822,19 @@ are always available. They are listed here in alphabetical order.
``range(start, stop, step)``. The *start* and *step* arguments default to
``None``.

Slice objects have read-only data attributes :attr:`!start`,
:attr:`!stop`, and :attr:`!step` which merely return the argument
values (or their default). They have no other explicit functionality;
however, they are used by NumPy and other third-party packages.
Slice objects are also generated when :ref:`slicing syntax <slicings>`
is used. For example: ``a[start:stop:step]`` or ``a[start:stop, i]``.

See :func:`itertools.islice` for an alternate version that returns an
:term:`iterator`.

.. attribute:: slice.start
.. attribute:: slice.stop
.. attribute:: slice.step
slice.stop
slice.step

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

.. versionchanged:: 3.12
Slice objects are now :term:`hashable` (provided :attr:`~slice.start`,
Expand Down
88 changes: 60 additions & 28 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ floating-point numbers. The same caveats apply as for floating-point numbers.
The real and imaginary parts of a complex number ``z`` can be retrieved through
the read-only attributes ``z.real`` and ``z.imag``.

.. _datamodel-sequences:

Sequences
---------
Expand All @@ -309,12 +310,25 @@ including built-in sequences, interpret negative subscripts by adding the
sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
item of sequence a with length ``n``.

.. index:: single: slicing
The resulting value must be a nonnegative integer less than the number of items
in the sequence. If it is not, an :exc:`IndexError` is raised.

Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
sequence of the same type. The comment above about negative indexes also applies
.. index::
single: slicing
single: start (slice object attribute)
single: stop (slice object attribute)
single: step (slice object attribute)

Sequences also support slicing: ``a[start:stop]`` selects all items with index *k* such
that *start* ``<=`` *k* ``<`` *stop*. When used as an expression, a slice is a
sequence of the same type. The comment above about negative subscripts also applies
to negative slice positions.
Note that no error is raised if a slice position is less than zero or larger
than the length of the sequence.

If *start* is missing or ``None``, slicing behaves as if *start* was zero.
If *stop* is missing or ``None``, slicing behaves as if *stop* was equal to
the length of the sequence.

Some sequences also support "extended slicing" with a third "step" parameter:
``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*
Expand Down Expand Up @@ -345,17 +359,22 @@ Strings
pair: built-in function; chr
pair: built-in function; ord
single: character
single: integer
pair: string; item
single: Unicode

A string is a sequence of values that represent Unicode code points.
All the code points in the range ``U+0000 - U+10FFFF`` can be
represented in a string. Python doesn't have a :c:expr:`char` type;
instead, every code point in the string is represented as a string
object with length ``1``. The built-in function :func:`ord`
A string (:class:`str`) is a sequence of values that represent
:dfn:`characters`, or more formally, *Unicode code points*.
All the code points in the range ``0`` to ``0x10FFFF`` can be
represented in a string.

Python doesn't have a dedicated *character* type.
Instead, every code point in the string is represented as a string
object with length ``1``.

The built-in function :func:`ord`
converts a code point from its string form to an integer in the
range ``0 - 10FFFF``; :func:`chr` converts an integer in the range
``0 - 10FFFF`` to the corresponding length ``1`` string object.
range ``0`` to ``0x10FFFF``; :func:`chr` converts an integer in the range
``0`` to ``0x10FFFF`` to the corresponding length ``1`` string object.
:meth:`str.encode` can be used to convert a :class:`str` to
:class:`bytes` using the given text encoding, and
:meth:`bytes.decode` can be used to achieve the opposite.
Expand All @@ -366,7 +385,7 @@ Tuples
pair: singleton; tuple
pair: empty; tuple

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

A bytes object is an immutable array. The items are 8-bit bytes,
A :class:`bytes` object is an immutable array. The items are 8-bit bytes,
represented by integers in the range 0 <= x < 256. Bytes literals
(like ``b'abc'``) and the built-in :func:`bytes` constructor
can be used to create bytes objects. Also, bytes objects can be
Expand Down Expand Up @@ -461,6 +480,8 @@ Frozen sets
a dictionary key.


.. _datamodel-mappings:

Mappings
--------

Expand Down Expand Up @@ -3219,28 +3240,39 @@ through the object's keys; for sequences, it should iterate through the values.
and so forth. Missing slice items are always filled in with ``None``.


.. method:: object.__getitem__(self, key)
.. method:: object.__getitem__(self, subscript)

Called to implement *subscription*, that is, ``self[subscript]``.
See :ref:`subscriptions` for details on the syntax.

There are two types of built-in objects that support subscription
via :meth:`!__getitem__`:

- **sequences**, where *subscript* (also called
*index*) should be an integer or a :class:`slice` object.
See the :ref:`sequence documentation <datamodel-sequences>` for the expected
behavior, including handling :class:`slice` objects and negative indices.
- **mappings**, where *subscript* is also called the *key*.
See :ref:`mapping documentation <datamodel-mappings>` for the expected
behavior.

Called to implement evaluation of ``self[key]``. For :term:`sequence` types,
the accepted keys should be integers. Optionally, they may support
:class:`slice` objects as well. Negative index support is also optional.
If *key* is
of an inappropriate type, :exc:`TypeError` may be raised; if *key* is a value
outside the set of indexes for the sequence (after any special
interpretation of negative values), :exc:`IndexError` should be raised. For
:term:`mapping` types, if *key* is missing (not in the container),
:exc:`KeyError` should be raised.
If *subscript* is of an inappropriate type, :meth:`!__getitem__`
should raise :exc:`TypeError`.
If *subscript* has an inappropriate value, :meth:`!__getitem__`
should raise an :exc:`LookupError` or one of its subclasses
(:exc:`IndexError` for sequences; :exc:`KeyError` for mappings).

.. note::

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

.. note::

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


Expand Down
Loading
Loading