Andrew Barnert added the comment:

> What are we exactly expecting here?

Well, naively, I was expecting a TypeError, just as you get for dict, or a 
subclass of dict, or a custom extension type that implements the C-API mapping 
protocol.

Once you understand how reversed works, you can understand why it gives you a 
nonsensical and useless iterator instead. But nobody would actually _want_ that.

So, as I proposed in the initial description, and the title, what we should be 
doing is raising a TypeError.

> How can a dictionary be reversed?

Assuming this is a pointless rhetorical question: That's exactly the point. 
There's no sensible meaning to reversing a dictionary, so it should raise a 
TypeError. Exactly as it already does for dict, subclasses of dict, and C-API 
mappings.

If this wasn't rhetorical: I guess you could argue that any arbitrary order in 
reverse is any arbitrary order, so even returning iter(m) would be acceptable. 
Or maybe reversed(list(m)) would be even better, if it didn't require O(N) 
space. But in practice, nobody will ever expect that--again, they don't get it 
from dict, subclasses, C-API mappings--so why go out of our way to implement 
it? So, again, it should raise a TypeError.

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

Of course. Again, the current behavior is nonsensical, will almost always raise 
a KeyError at some point, and will never be anything a reasonable person wants. 
So a workaround that restores the current behavior will also be nonsensical, 
almost always raise a KeyError at some point, and never be anything a 
reasonable person wants.

But, if you happen to be unreasonably unreasonable--say, you created a mapping 
with {2:40, 0:10, 1:20} and actually wanted reversed(m) to confusingly give you 
40, 20, 10--and this change did break your code, the workaround would restore 
it.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25864>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to