[Python-ideas] Where did we go wrong with negative stride?

Tim Peters tim.peters at gmail.com
Sun Oct 27 22:56:34 CET 2013


[Guido]
> I wouldn't take out negative strides completely, but I might consider
> deprecating lower and upper bounds other than None (== missing). So a[::-1]
> would still work, and a[None:None:-1] would be a verbose way of spelling the
> same,

Happy idea.

> but a[-1:-6:-1] would be deprecated.

Not sure I've _ever_ seen that in real life.  Where it comes up is on
places like stackoverflow, when somebody (mistakely) suggests using
seq[:-1:-1] to do a reverse slice.  Then it's pointed out that this
doesn't work like range(len(seq), -1, -1).  Then some wiseass with too
much obscure knowledge of implementation details ;-) points out that
seq[:-len(seq)-1:-1] does work (well, in CPython - I don't know
whether all implementations follow this quirk - although the docs
imply that they should).

> Then we could triumphantly (re-)introduce upper and lower bounds in Python 4, with
> the meaning a[i:j:-1] == a[i:j][::-1].

+1.


More information about the Python-ideas mailing list