summaryrefslogtreecommitdiff
path: root/Doc/reference/datamodel.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/reference/datamodel.rst')
-rw-r--r--Doc/reference/datamodel.rst14
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 6acd25a760..9c2d7cd584 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1688,6 +1688,20 @@ container; for mappings, :meth:`__iter__` should be the same as
Iterator objects also need to implement this method; they are required to return
themselves. For more information on iterator objects, see :ref:`typeiter`.
+
+.. method:: object.__reversed__(self)
+
+ Called (if present) by the :func:`reversed` builtin to implement
+ reverse iteration. It should return a new iterator object that iterates
+ over all the objects in the container in reverse order.
+
+ If the :meth:`__reversed__` method is not provided, the
+ :func:`reversed` builtin will fall back to using the sequence protocol
+ (:meth:`__len__` and :meth:`__getitem__`). Objects should normally
+ only provide :meth:`__reversed__` if they do not support the sequence
+ protocol and an efficient implementation of reverse iteration is possible.
+
+
The membership test operators (:keyword:`in` and :keyword:`not in`) are normally
implemented as an iteration through a sequence. However, container objects can
supply the following special method with a more efficient implementation, which