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

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Oct 30 15:42:43 CET 2013


On 30 October 2013 13:45, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 30 October 2013 20:22, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> 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

It took me a while to get to that link. I think bitbucket may be
having server problems.

>>>> 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)

I like the idea of a magic End object. I would be happy to see
negative indexing deprecated in favour of that. For this to really be
useful though it needs to apply to ordinary indexing as well as
slicing. If it also becomes an error to use negative indices then you
get proper bounds checking as well as an explicit way to show when
you're indexing from the end which is a substantial improvement.


Oscar


More information about the Python-ideas mailing list