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

Michael Hudson mwh21@cam.ac.uk
01 Aug 2000 07:59:11 +0100


"Fredrik Lundh" <effbot@telia.com> writes:

> greg wrote:
> 
> > I think there are some big conceptual problems with allowing
> > negative steps in a slice.
> 
> wasn't "slices" supposed to work the same way as "ranges"?

The problem is that for slices (& indexes in general) that negative
indices have a special interpretation:

range(10,-1,-1)
range(10)[:-1]

Personally I don't think it's that bad (you just have to remember to
write :: instead of :-1: when you want to step all the way back to the
beginning).  More serious is what you do with out of range indices -
and NumPy is a bit odd with this one, it seems:

>>> l = Numeric.arrayrange(10)
>>> l[30::-2]
array([0, 8, 6, 4, 2, 0])

What's that initial "0" doing there?  Can someone who actually
understands NumPy explain this?

Cheers,
M.

(PS: PySlice_GetIndices is in practice a bit useless because when it
fails it offers no explanation of why!  Does any one use this
function, or should I submit a patch to make it a bit more helpful (&
support longs)?)

-- 
    -Dr. Olin Shivers,
     Ph.D., Cranberry-Melon School of Cucumber Science
                                           -- seen in comp.lang.scheme