The extended indexing syntax is mentioned in python’s doc.
slice([start], stop[, step])
Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.
a[start:stop:step] works as described. But what about the second one? How is it used?
a[start:stop,i]calls the methoda.__getitem__((slice(start,stop,None), i)).This raises a
TypeErrorifais a list, but it is valid and useful notation ifais a numpy array. In fact, I believe the developers of Numpy asked the developers of Python to extend valid Python slicing notation precisely so that numpy array slicing notation could be implemented more easily.For example,
1:3selects rows 1 and 2, and the2selects the third column:PS. To experiment with what slice is getting sent to
__getitem__, you canplay around with this toy code: