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

אלעזר elazarg at gmail.com
Thu Oct 31 14:37:36 CET 2013


2013/10/31 Ethan Furman <ethan at stoneleaf.us>:
> On 10/30/2013 06:36 PM, MRAB wrote:
>>
>> On 31/10/2013 00:05, אלעזר wrote:
>>>
>>> 2013/10/31 MRAB <python at mrabarnett.plus.com>:
>>>>
>>>> On 30/10/2013 23:00, Eric Snow wrote:
>>>>>
>>>>>
>>>>> On Wed, Oct 30, 2013 at 4:47 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>>>>>>
>>>>>>
>>>>>> I though of using a magic symbol, $, for that -- a[$-n]. But aside
>>>>>> from
>>>>>> the issue of using one of the 2 remaining unused ascii symbols for
>>>>>> something that can already be done, it would not work in a slice call.
>>>>>
>>>>>
>>>>>
>>>>> Is that like where you have 1 more shot on your camera and you don't
>>>>> want to use it for fear that something more spectacular might show up
>>>>> afterward?  (and hope that you didn't leave your lens cap on when you
>>>>> finally take the picture!)  :-)
>>>>>
>>>> I don't think it's that bad; I count 3: "!", "$" and "?". :-)
>>>>
>>> Can't it be done by adding a __sub__ method to len?
>>>
>>> a[:len-n]
>>>
>>> Readable and short.
>>>
>> -1
>>
>> I don't like how it makes that function special.
>
>
> Not only that, but len wouldn't know what it was subtracting from.
>
But that doesn't matter; the operation will return the same End object
discussed here.

Perhaps we can get this End object by adding two tokens: ":-" and "[-". So

a[-3:-5] == a[slice(End-3, End-5, None)]

although it will turn a[-3] into a[End-3]. I don't think it's a
problem if the latter will behave in the same way as the former (i.e
End-3 be a subtype of int).

Note that with an End object (regardless of wheather it's called
"End", "len-x" or ":-x") we can get End/5. I think that's a nice thing
to have.

One more thing: End-5 should be callable, so it can be passed around.

(End-3)("hello") == len("hello")-3
(End-0)("hello") == len("hello")

This way End is a generalization of len, making len somewhat redundant.


More information about the Python-ideas mailing list