Slice objects with negative increment

Fernando Pérez fperez528 at yahoo.com
Mon Apr 29 17:47:55 EDT 2002


Paul Hughett wrote:

> I am writing a Python extension in which it has become useful to
> define slice objects that run backwards (i.e. that have a negative
> increment) and want to use the existing convention, if any, for
> such objects.  But I don't recall ever seeing such a convention.
> 
> Hence my question: Is there an established meaning within Python for
> slice objects with a negative increment?  E.g. slice(1, 10, -2) ?

Yes, the standard Numeric already has well established conventions:

In [6]: z=arange(10)

In [7]: z
Out[7]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [8]: z[::-3]
Out[8]: array([9, 6, 3, 0])

Numeric is standard enough that I wouldn't even consider breaking its 
conventions (esp. since it's the only widely known Python module which fully 
implements slice objects).

hth,

f



More information about the Python-list mailing list