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

Georg Brandl g.brandl at gmx.net
Mon Oct 28 06:33:33 CET 2013


Am 27.10.2013 22:56, schrieb Tim Peters:
> [Guido]
>> I wouldn't take out negative strides completely, but I might consider
>> deprecating lower and upper bounds other than None (== missing). So a[::-1]
>> would still work, and a[None:None:-1] would be a verbose way of spelling the
>> same,
> 
> Happy idea.
> 
>> but a[-1:-6:-1] would be deprecated.
> 
> Not sure I've _ever_ seen that in real life.  [...]

Before such a change is considered, I'd like to see the numpy community
consulted; numpy users probably use slicing more than anyone else.  At
least it should be possible to integrate the new slicing without breaking
too many other numpy behavior.

As a datapoint, Matlab negative-stride slicing is similar to Python, but
it is less confusing (IMO) since the slices are inclusive on both ends.
Let "a" be a range from 1 to 10:

> a(2:7)        % slicing is done with parens; 1-based indexing
[2 3 4 5 6 7]
> a(2:2:7)      % the stride is the middle value
[2 4 6]
> a(2:-1:7)     % same as in Python
[]
> a(7:-1:2)     % a(i:-1:j) == reverse of a(j:1:i) due to end-inclusive
[7 6 5 4 3 2]
> a(7:-2:2)     % but obviously not so for non-unity stride
[7 5 3]

cheers,
Georg





More information about the Python-ideas mailing list