[New-bugs-announce] [issue6034] Fix object.__reversed__ doc

Terry J. Reedy report at bugs.python.org
Sat May 16 05:18:08 CEST 2009


New submission from Terry J. Reedy <tjreedy at udel.edu>:

3.3.5. Emulating container types
object.__reversed__(self)
says in 3.0 and 3.1 and I assume in 2.x:
"Objects should normally only provide __reversed__() if they do not
support the sequence protocol and an efficient implementation of reverse
iteration is possible."

The builtin sequences violate this because because they do support the
sequence and have __ reversed__ methods anyway.  And iterables that do
not support that protocol obviously *must* provide a method to be
reverse iterable.

I believe the point is that it is hard for Python code to beat the
C-coded version of the obvious

def __reversed__(self):
   for i in reversed(range(self.__len__)):
      yield self.__getitem__(i)

So I think the entry should say: "Objects that support the sequence
protocol should only provide __reversed__ if they can provide an
implementation that is more efficient than the one provided by
reversed()." possibly followed by "Objects that do not supposrt the
sequence protocol must provide __reversed__ to be reverse iterable."

----------
assignee: georg.brandl
components: Documentation
messages: 87852
nosy: georg.brandl, tjreedy
severity: normal
status: open
title: Fix object.__reversed__ doc
versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6034>
_______________________________________


More information about the New-bugs-announce mailing list