[Tutor] What exactly is [::-1]?

Kent Johnson kent37 at tds.net
Thu Jul 26 12:40:27 CEST 2007


Alan Gauld wrote:
> "wesley chun" <wescpy at gmail.com> wrote
> 
>> when you use the 3rd element, it's called the extended slice syntax.
> 
> The ref manual describes use of the third value as simple slicing,
> for extended slicing it says this:

<snip>

> I've read it three times now and stioll have no idea what its on 
> about!

Extended slicing is used by Numeric and its successors to slice 
multi-dimensional arrays on multiple dimensions. An extended slice is a 
simple slice for each dimension of the array, separated by commas. See
http://numpy.scipy.org/numpydoc/numpy-6.html#pgfId-36074

AFAIK extended slicing is not supported by any standard Python data 
types, it was added specifically for Numeric.

When an object is indexed with an extended slice, the object's 
__getitem__() method is passed a tuple of slice objects. It's then up to 
the object to make sense of it:

In [1]: class ext(object):
    ...:     def __getitem__(self, key):
    ...:         print repr(key)
    ...:
    ...:
In [2]: e=ext()
In [3]: e[3]
3
In [4]: e[1:4]
slice(1, 4, None)
In [5]: e[1:4,2:5]
(slice(1, 4, None), slice(2, 5, None))

Kent


More information about the Tutor mailing list