Is there something internal in python that treats arguments passed to __getitem_
_
differently, and automatically converts start:stop:step
constructs into slices?
Here's a demonstration of 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 it simply that the interpreter searches for instances of start:stop:step
inside []
, and swaps them out for slice(start, stop, step)
? The documentation simply says:
The bracket (subscript) notation uses slice objects internally
Is this one of the python internal bits that i can't alter the behaviour of? Is it possible to make other functions take slice objects usign the start:stop:step
shorthand?*
*I've seen the other question, Can python's slice notation be used outside of brackets?, but that just does it using a custom class, which i could easily do. What i want is a way to just use start:stop:step
without having to wrap it in anything else.
SIDE NOTE:
It also apears that all arguments inside [...]
are packaged up into a tuple
, somewhat as if it were doing [*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)