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

Nick Coghlan ncoghlan at gmail.com
Wed Oct 30 14:45:49 CET 2013


On 30 October 2013 20:22, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 30 October 2013 20:13, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
>> But you currently need to write it that way to get the correct behaviour:
>>
>>>>> for n in reversed(range(4)):
>> ...     print(n, a[len(a)-n:])
>> ...
>> 3 cde
>> 2 de
>> 1 e
>> 0
>
> Regardless, my main point is this: slices are just objects. The syntax:
>
>    s[i:j:k]
>
> is just syntactic sugar for:
>
>   s[slice(i, j, k)]
>
> That means that until people have fully explored exactly the semantics
> they want in terms of the existing object model, just as I did for
> rslice(), then there are *zero* grounds to be discussing syntax
> changes that provide those new semantics.

Hmm, looks like my rslice testing was broken. Anyway, I created an
enhanced version people using the "End - idx" notation from the end
that actually passes more systematic testing:

https://bitbucket.org/ncoghlan/misc/src/default/rslice.py?at=default

>>> from rslice import rslice, betterslice, End
>>> betterslice(-4, 5)
slice(0, 5, 1)
>>> betterslice(End-4, 5)
slice(-4, 5, 1)
>>> rslice(-4, 5).as_slice(10)
slice(4, -11, -1)
>>> rslice(End-4, 5).as_slice(10)
slice(4, -5, -1)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list