[Python-Dev] Negative slice steps considered unhealthy (extended slicing for lists)

Greg Ewing greg@cosc.canterbury.ac.nz
Tue, 01 Aug 2000 11:45:02 +1200 (NZST)


I think there are some big conceptual problems with allowing
negative steps in a slice.

With ordinary slices, everything is very clear if you think
of the indices as labelling the points between the list
elements.

With a step, this doesn't work any more, and you have to
think in terms of including the lower index but excluding the
upper index.

But what do "upper" and "lower" mean when the step is negative?
There are several things that a[i:j:-1] could plausibly mean:

   [a[i], a[i-1], ..., a[j+1]]

   [a[i-1], a[i-2], ..., a[j]]

   [a[j], a[j-1], ..., a[i+1]]

   [a[j-1], a[j-2], ..., a[i]]

And when you consider negative starting and stopping values,
it just gets worse. These have no special meaning to range(),
but in list indexing they do. So what do they mean in a slice
with a step? Whatever is chosen, it can't be consistent with
both.

In the face of such confusion, the only Pythonic thing would
seem to be to disallow these things.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+