]> granicus.if.org Git - python/commitdiff
#1582: document __reversed__, patch by Mark Russell.
authorGeorg Brandl <georg@python.org>
Sun, 6 Jan 2008 16:17:56 +0000 (16:17 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 6 Jan 2008 16:17:56 +0000 (16:17 +0000)
Doc/ACKS.txt
Doc/library/functions.rst
Doc/reference/datamodel.rst

index 32943bddd5203ca6bae5616b1eb5db1d8ea906bd..7344cf8dac47441ac2d9f89d908d253132284a72 100644 (file)
@@ -158,6 +158,7 @@ docs@python.org), and we'll be glad to correct the problem.
    * Jim Roskind
    * Guido van Rossum
    * Donald Wallace Rouse II
+   * Mark Russell
    * Nick Russo
    * Chris Ryland
    * Constantina S.
index 4f86fc71c6fb27f7b3b3a929e0191b74070fd3e3..693af645638cab441d512f308c70ab458fb2a1b5 100644 (file)
@@ -977,12 +977,16 @@ available.  They are listed here in alphabetical order.
 
 .. function:: reversed(seq)
 
-   Return a reverse :term:`iterator`.  *seq* must be an object which supports
-   the sequence protocol (the :meth:`__len__` method and the :meth:`__getitem__`
-   method with integer arguments starting at ``0``).
+   Return a reverse :term:`iterator`.  *seq* must be an object which has
+   a :meth:`__reversed__` method or supports the sequence protocol (the
+   :meth:`__len__` method and the :meth:`__getitem__` method with integer
+   arguments starting at ``0``).
 
    .. versionadded:: 2.4
 
+   .. versionchanged:: 2.6
+      Added the possibility to write a custom :meth:`__reversed__` method.
+
 
 .. function:: round(x[, n])
 
index 7ea6ca757a905f3cd298533595e8ebfbf9d1cbae..ed05b322c88681496c4a7bac943971b7895d39db 100644 (file)
@@ -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