python中是否存在某种内部机制,它以不同的方式处理传递给__getitem_
_的参数,并自动将start:stop:step构造转换为片?在
这是我的意思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))
解释器是否只是在[]内搜索start:stop:step的实例,并将它们交换为slice(start, stop, step)?文件只简单地说:The bracket (subscript) notation uses slice objects internally
这是python内部的一个部分,我不能改变它的行为吗?是否可以让其他函数使用start:stop:step速记获取切片对象?*在
*我已经看到了另一个问题,Can python's slice notation be used outside of brackets?,但这只是使用一个自定义类,我可以很容易地做到这一点。我想要的是一种只使用start:stop:step而不必将其包装在其他任何东西中的方法。在
旁注:
它还表示[...]中的所有参数都打包成tuple,有点像它在做[*args]->;__getitem__(args)。在
^{pr2}$