summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-01-06 16:17:56 (GMT)
committerGeorg Brandl <georg@python.org>2008-01-06 16:17:56 (GMT)
commit81de0d24d589251d9465b3a214a9b9adb3962f4a (patch)
tree73e32ee3ced3ed3c1570df034ca3f6df32d91bbe /Doc/reference
parentec32b6bce7a9d2c58d1ad040c2cdca34de819634 (diff)
downloadcpython-81de0d24d589251d9465b3a214a9b9adb3962f4a.zip
cpython-81de0d24d589251d9465b3a214a9b9adb3962f4a.tar.gz
cpython-81de0d24d589251d9465b3a214a9b9adb3962f4a.tar.bz2
#1582: document __reversed__, patch by Mark Russell.
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/datamodel.rst16
1 files changed, 16 insertions, 0 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 7ea6ca7..ed05b32 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1796,6 +1796,22 @@ sequences, it should iterate through the values.
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.
+
+ .. versionadded:: 2.6
+
+
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