Message367147
> `Mapping.__reversed__` exists
While ``'__reversed__' in dir(Mapping)`` is true, that unfortunately does not mean that it is a real callable method:
from collections.abc import Mapping
class Map(Mapping):
def __getitem__(self, x):
if x == 42:
return 17
raise KeyError
def __len__(self, x):
return 1
def __iter__(self):
yield 42
>>> m = Map()
>>> reversed(m)
Traceback (most recent call last):
...
TypeError: 'Map' object is not reversible
In the code for Mapping, ``__reversed__`` is explicitly defined as None [1] so that calling ``reversed(custom_mapping)`` doesn't accidentally fall back on the sequence protocol, which would mean starting at len(custom_mapping)-1 and calling __getitem__ on each index [2], which is certainly not desirable for arbitrary mappings.
I don't think there is a reasonable way, given arbitrary implementations of __len__, __iter__, and __getitem__, to have a mixin reversed iterator. If someone wants their mapping to have a __reversed__ method, they should define it themselves.
[1] https://github.com/python/cpython/blob/master/Lib/_collections_abc.py#L707
[2] https://docs.python.org/3/reference/datamodel.html?highlight=__reversed__#object.__reversed__ |
|
Date |
User |
Action |
Args |
2020-04-23 21:27:58 | Dennis Sweeney | set | recipients:
+ Dennis Sweeney, rhettinger, stutzbach, docs@python, Thor Whalen2 |
2020-04-23 21:27:58 | Dennis Sweeney | set | messageid: <1587677278.15.0.840790535156.issue40374@roundup.psfhosted.org> |
2020-04-23 21:27:58 | Dennis Sweeney | link | issue40374 messages |
2020-04-23 21:27:57 | Dennis Sweeney | create | |
|