Call reversed
on a list:
>>> reversed([1, 2, 3])
<list_reverseiterator object at 0x102a84908>
Now, call it on another iterable..., say a tuple:
>>> reversed((1, 2, 3))
<reversed object at 0x102a848d0>
The first bit of code yields a list_reverseiterator object
while the second yields reversed object
.
Similar behaviour observed across all pythons. So, why is there a difference?
From the docs,
Return a reverse iterator. seq must be an object which has a
__reversed__()
method or supports the sequence protocol (the__len__()
method and the__getitem__()
method with integer arguments starting at 0).
Does this have something to do with whether __reversed__
is implemented or not? Because...
>>> list.__reversed__
<method '__reversed__' of 'list' objects>
But
>>> tuple.__reversed__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'tuple' has no attribute '__reversed__'