练习为学习pythonoop而发布的here示例。我正在查找“1到4”的输出,但它引发以下错误。在
class FakeList:
def __getslice___(self,start,end):
return str(start) + " to " + str(end)
f = FakeList()
f[1:4]
注意:使用f.__getitem__(1, 4)
会得到正确的输出--“1到4”,如上面的链接所示。在
Traceback (most recent call last) in () ----> 1 f[1:4]
TypeError: 'FakeList' object is not subscriptable
如注释中所述,}访问范围的开始/结束,下面是一个示例,其中有一些更多的调试输出,以显示正在进行的操作:
__getitem__
方法接受一个slice
类型的参数,您可以通过slice.start
和{输出:
^{pr2}$相关问题 更多 >
编程相关推荐