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

אלעזר elazarg at gmail.com
Sun Oct 27 18:23:39 CET 2013


I believe the problem is not about negative strides but about negative
bounds. There should be a notion of "minus zero", something like

"abcde"[:-0:-1] =="edcba".

Here ":-" serves as a special syntax for negative stride; of course it is
not a real proposal.
The same awkwardness results when you take a negative upper bounds to the
limit of 0:

"abcde"[:-2] == "abc"
"abcde"[:-1] == "abcd"
"abcde"[:-0] == ""

(I once filed a bug for it, which was of course correctly rejected:
http://bugs.python.org/issue17287).

Elazar


2013/10/27 Guido van Rossum <guido at python.org>

> In the comments of
> http://python-history.blogspot.com/2013/10/why-python-uses-0-based-indexing.htmlthere were some complaints about the interpretation of the bounds for
> negative strides, and I have to admin it feels wrong. Where did we go
> wrong? For example,
>
> "abcde"[::-1] == "edcba"
>
> as you'd expect, but there is no number you can put as the second bound to
> get the same result:
>
> "abcde"[:1:-1] == "edc"
> "abcde"[:0:-1] == "edcb"
>
> but
>
> "abcde":-1:-1] == ""
>
> I'm guessing it all comes from the semantics I assigned to negative stride
> for range() long ago, unthinkingly combined with the rules for negative
> indices.
>
> Are we stuck with this forever? If we want to fix this in Python 4 we'd
> have to start deprecating negative stride with non-empty lower/upper bounds
> now. And we'd have to start deprecating negative step for range()
> altogether, recommending reversed(range(lower, upper)) instead.
>
> Thoughts? Is NumPy also affected?
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131027/d4157d47/attachment.html>


More information about the Python-ideas mailing list