Issue 6034: Fix object.reversed doc (original) (raw)

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