Python and __getitem__ slice objects

Is there anything internal in python that handles arguments passed in __getitem_ _different ways and automatically converts constructs start:stop:stepto slices?

It demonstrates what I mean

class ExampleClass(object):

  def __getitem__(self, *args):
    return args

  def __call__(self, *args):
    return args

  def randomMethod(self, *args):
    return args


a = ExampleClass()

#this works
print a[3:7:2, 1:11:2]

#syntax error on the first colon
print a.randomMethod(3:7:2, 1:11:2)
print a(3:7:2, 1:11:2)

#these work
print a.randomMethod(slice(3,7,2), slice(1,11,2))
print a(slice(3,7,2), slice(1,11,2))

Is the interpreter just looking for instances start:stop:stepinside []and replacing them with slice(start, stop, step)? The documentation just says:

The parenthesis (index) designation uses slice objects inside

Is this one of the internal bits of python that I cannot change the behavior? Is it possible for other functions to take slice objects, cut out the sash start:stop:step? *

* , python ?, , . , , - start:stop:step, .

:

, [...] tuple, [*args]__getitem__(args).

class ExampleClass2(object):

  def __getitem__(self, arg):
    return arg

  def __call__(self, arg):
    return arg


b = ExampleClass2()

print b["argument 1", 2:4:6,3] # ('argument 1', slice(2, 4, 6), 3)
print b(slice(3,7,2), slice(1,11,2)) # TypeError: __call__() takes exactly 2 arguments (3 given)
+4
2

Python , :

trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
subscript: test | [test] ':' [test] [sliceop]
sliceop: ':' [test]

test - , subscriptlist . , , , , , , , , .

, -, slice(a,b,c).

+4

np.lib.index_tricks "", ::, . np.mgrid, np.r_, np.s_.

__getitem__. "".

np.s_[2::2] #  slice(2, None, 2)
np.r_[-1:1:6j, [0]*3, 5, 6]  # array([-1. , -0.6, -0.2,  0.2, ... 6. ])
mgrid[0:5,0:5]

, __getitem__.

np.insert - , , . np.apply_along :

i = zeros(nd, 'O')
...
i[axis] = slice(None, None)
...
i.put(indlist, ind)
...arr[tuple(i.tolist())]

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html :

, obj x[obj]. [start:stop:step]. , x[1:10:5,::-1] obj = (slice(1,10,5), slice(None,None,-1)); x[obj]. , .

+2

All Articles