[issue16712] collections.abc.Sequence should not provide __reversed__

Raymond Hettinger report at bugs.python.org
Sun Dec 23 22:23:53 CET 2012


Raymond Hettinger added the comment:

Guido put a number of non-optimal implementations in the ABCs.  His goal was to define the interface and to supply a working default implementation (see MutableMapping.clear() for a prime example).

In the case of __reversed__(), it is unfortunate that it slows down the default implementation of reverse().  The latter only runs faster because it is in C, not because of any algorithmic issue.

FWIW, the same is also true of Sequence.__contains__().  This logic in the ABC is straight-forward but slows down the code as compared to Python's C optimizations which can infer a __contains__ method from a sequence that defines __getitem__().

Given that the "issue" isn't algorithmic and is merely a C vs pure Python issue, I recommend leaving the current code as-is.

If someone truly cares about the speed issue, it would be an easy matter to supply a C helper function in the ABCs for speeding-up __reversed__ and __contains__ (for an example of how to do this, see _count_elements() in the collections module).

----------

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


More information about the Python-bugs-list mailing list