a[start:stop,i] calls the a.__getitem__((slice(start,stop,None), i)) method.
This raises a TypeError if a is a list, but it is a correct and useful notation if a is an empty array. In fact, I believe that Numpy developers asked Python developers to accurately extend the actual Python slice notation to make it easier to implement array slice notation.
For example,
import numpy as np arr=np.arange(12).reshape(4,3) print(arr)
1:3 selects rows 1 and 2, and 2 selects the third column:
print(arr[1:3,2])
PS. To experiment with which snippet is sent to __getitem__ , you can play __getitem__ with this toy code:
class Foo(list): def __getitem__(self,key): return repr(key) foo=Foo(range(10)) print(foo[1:5,1,2])
unutbu
source share