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

Bruce Leban bruce at leapyear.org
Mon Oct 28 20:26:43 CET 2013


On Mon, Oct 28, 2013 at 11:49 AM, Tim Peters <tim.peters at gmail.com> wrote:

> As a Python implementer, _I_ do, but not as a user.  As Guido noted,
> under the proposal we have:
>
>     s[i:j:k] == s[i:j][::k]
>
> That should (finally?) make it crystal clear that applying the stride
> has nothing directly to do with the indices of the selected elements
> in the original sequence (`s`).


It's definitely not "finally clear" as it's a change in semantics. What
about negative strides other than -1? Which of these is expected?

(A) '012345678'[::-2] == '86420'
    '0123456789'[::-2] == '97531'

or:

(B) '012345678'[::-2] == '86420'
    '0123456789'[::-2] == '86420'

If (A) I can get the (B) result by writing [::2][::-1] but if (B), I'm
forced to write:

s[0 if len(s) % 2 == 1 else 1::2]

or something equally ugly.

--- Bruce

(Also, (A) is the current behavior and switching to (B) would break any
existing use of strides < -1.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131028/cd87f881/attachment.html>


More information about the Python-ideas mailing list