[issue25864] collections.abc.Mapping should include a __reversed__ that raises TypeError

Swati Jaiswal report at bugs.python.org
Mon Dec 14 23:44:21 EST 2015


Swati Jaiswal added the comment:

> If you do `reversed(d)`, you get a nice `TypeError: argument to reversed() must be a sequence`. But if you do `reversed(m)`, you get a reversed` iterator. And when you iterate it, presumably expecting to get 0 and 1 in some arbitrary order, you instead get 3, and then a KeyError:0`.

I got 2 instead of 3.

What are we exactly expecting here? How can a dictionary be reversed?

> I can't imagine this would break any working code. If it did, the workaround would be simple: just implement `def __reversed__(self): return (self[k] for k in reversed(range(len(self))))`.

This seems to make no difference. I still got the KeyError.

----------

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


More information about the Python-bugs-list mailing list