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

Ron Adam ron3200 at gmail.com
Sun Oct 27 21:02:25 CET 2013


On 10/27/2013 01:32 PM, Guido van Rossum wrote:


> Still, the invariant for negative stride looks less attractive, and the
> need to use negative indices confuses the matter. Also we end up with -1
> corresponding to the position at one end and -len(a)-1 corresponding to the
> position at the other end. The -1 offset feels really wrong here.

And I've never liked the property where when counting down, and you pass 0, 
it wraps around.   (And the other case of counting up when passing 0.)


> I wonder if it would have been simpler if we had defined a[i:j:-1] as the
> reverse of a[i:j]?

I think that would have been simpler.


Could adding an __rgetitem__() improve things?

      seq[i:j:k]   -->  __getitem__(slice(i:j:k))

      seq-[i:j:k]   -->  __rgetitem__(slice(i:j:k))

Or the sign of K could determine weather __getitem__ or __rgetitem__ is used?


Ron



More information about the Python-ideas mailing list