Andrew Barnert added the comment:

@R. David Murray:

> What is it that makes reversed raise a typeerror on dict here?

There are separate slots for tp_as_sequence and tp_as_mapping, so a C type can 
be (and generally is) one or the other, not both. 

But for Python types, anything that has __getitem__ is both a sequence and a 
mapping at the C level. (It's one of the few minor inconsistencies between C 
and Python types left, like having separate slots for nb_add and sq_concat in C 
but only __add__ for both in Python.)

> Not that we can change it at this point, but reversed blindly using len and 
> __getitem__ for user classes but not on dict is rather inconsistent.  

But it's consistent with iter blindly using __len__ and __getitem__ if __iter__ 
is not present on Python classes but not doing that on C classes. That's how 
"old-style sequences" work, and I don't think we want to get rid of those (and, 
even if we did, I'm pretty sure that would require at least a thread on -dev or 
-ideas...).

> I suppose the dict TypeError special case catches common mistakes?

Yes. That's probably not why it was implemented (it's easier for a C type to 
_not_ fake being a broken sequence than to do so), but it has that positive 
effect.

> In which case adding a __reversed__ that raises a TypeError to Mapping seems 
> to make sense for the same reason.

Exactly.

I think Raymond's point is that, while it does make sense, it may still not be 
important enough to be worth even two lines of code. Hopefully we can get more 
than two opinions (his and mine) on that question; otherwise, at least as far 
as I'm concerned, he trumps me.

----------

_______________________________________
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