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

Andrew Barnert abarnert at yahoo.com
Tue Oct 29 05:15:52 CET 2013


On Oct 28, 2013, at 17:41, Tim Peters <tim.peters at gmail.com> wrote:

> [Ron Adam]
>>> We also need to remember that slicing is also used for inserting things.
>>> 
>>>>>> a = list("python")
>>>>>> b = list("PYTHON")
>>>>>> a[::2] = b[::2]
>>>>>> a
>>> ['P', 'y', 'T', 'h', 'O', 'n']
> 
> [Andrew Barnert]
>> I was about to write the same thing. Half the mails so far have said things like "you don't
>> need to do [i: j:k] because you can do [m:n:o][::p]".
> 
> You must be missing most of the messages, then ;-)

For example, the whole sub discussion starting with Bruce Leban's post, which I'll quote here:

> (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]

The idea is that A (today's behavior) is fine because you can get the B result (one of the proposals, which Bruce apparently didn't like) with two slices. 

Someone then pointed out that the proposal is equally fine because, despite what Bruce suggested, you can get the A result with two slices.

But if you take away the ability to specify A in a single slice, you take away the ability to assign to A.

Imagine I'd written s[:-5:-2]=1, 3 (or equivalently s[:5:-2]) and the language changed to make this now replace the 8 and 6 instead of the 9 and 7. How would I change my code to get the previous behavior back?

It's quite possible no one has ever intentionally written such code. Or, even if they _have_, that they shouldn't have. (You can hardly call something readable if you have to sit down and work through what it would do to various sequences.) And correctly supporting such nonexistent code has been a burden on every custom sequence ever implemented.

So, a proposal that makes strides < -1 with non-None end points into an error (as some of them have) seems reasonable.

But I think a proposal that changes the meaning of such slices into something different (as some of them have) is a lot riskier. (Especially since many custom sequences that didn't implement slice assignment in the clever way would have to be rewritten.)

>> But that doesn't work with assignment; you're just assigning to the temporary copy
>> of the first slice.
>> ...
> 
> Do you have a specific example of a currently-working slice assignment
> that couldn't easily be done under proposed alternatives?

s[:-4:-2]=1, 2

This replaces the last and antepenultimate elements, whether s is even or odd.

I suppose you could mechanically convert it to this:

s[-mid+2::2]=reversed((1,2))

But I don't know that I'd call that "easy".

The question is whether this is realistic code anyone would ever intentionally write.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131028/0a8f5144/attachment.html>


More information about the Python-ideas mailing list