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."
|