Skip to content
Prev Previous commit
Next Next commit
Revise changes to __getitem__
  • Loading branch information
AlexWaygood committed Nov 7, 2021
commit ca568d5eec128f79647ac3b7f74c238a3ee3b1d4
20 changes: 10 additions & 10 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2443,27 +2443,27 @@ through the object's keys; for sequences, it should iterate through the values.

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

Called to implement evaluation of ``self[key]``, which is translated by the
interpreter to ``type(self).__getitem__(self, key)``. For :term:`sequence`
types, the accepted keys should be integers and slice objects. Note that
the special interpretation of negative indexes (if the class wishes to
emulate a :term:`sequence` type) is up to the :meth:`__getitem__` method. If
*key* is of an inappropriate type, :exc:`TypeError` may be raised; if of a
value outside the set of indexes for the sequence (after any special
Called to implement evaluation of ``self[key]``. For :term:`sequence` types,
the accepted keys should be integers and slice objects. Note that the
special interpretation of negative indexes (if the class wishes to emulate a
:term:`sequence` type) is up to the :meth:`__getitem__` method. If *key* is
of an inappropriate type, :exc:`TypeError` may be raised; if of 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.

.. 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.
:keyword:`for` loops expect that an :exc:`IndexError` will be raised for
illegal indexes to allow proper detection of the end of the sequence.

.. note::

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`.
``__getitem__()``. See :ref:`classgetitem-versus-getitem` for more
details.


.. method:: object.__setitem__(self, key, value)
Expand Down