如何在Python 3中实现切片?
我读到了一些关于 Python 3 中 slice 的内容。然后我编写了一个程序,尝试实现 __getitem__(self, slice(s)) 。代码如下:
class NewList:
def __init__(self, lst):
print('new list')
self._list = lst
def __getitem__(self, x):
if type(x) is slice:
return [ self._list[n] for n in range(x.start, x.stop, x.step) ] #error?
else:
return self._list[x]
...
nl1 = NewList([1,2,3,4,5])
nl1[1:3] #error occurs
然后我发现x.step
是None
,这使得range引发异常。 那么,我应该如何实现__getitem__
方法呢?
I read something about slice in Python 3. Then I wrote a program, tried to implement __getitem__(self, slice(s))
. Code goes below:
class NewList:
def __init__(self, lst):
print('new list')
self._list = lst
def __getitem__(self, x):
if type(x) is slice:
return [ self._list[n] for n in range(x.start, x.stop, x.step) ] #error?
else:
return self._list[x]
...
nl1 = NewList([1,2,3,4,5])
nl1[1:3] #error occurs
Then I found out x.step
is None
, which made range raise an exception.
So, how should I implement the __getitem__
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用
slice.indices
方法。给定序列的长度,它返回一个包含开始、停止、步骤的元组:You need to use the
slice.indices
method. Given the length of your sequence, it returns a tuple of start, stop, step:在您不知道对象长度的情况下,有一个明显的技巧可以绕过这个强制参数。例如,无限序列的 getitem 可以如下所示:
如果您不给出开始和停止,它只会失败,但通过检查 None 这也可以处理。
In the case where you don't know the length of your object there is an obvious trick to circumvent this mandatory parameter. For example an infinite sequence's getitem can look like this:
It will only fail if you don't give start and stop but with checking for None this could be handled too.
如果
x
是一个切片,则可以执行与其他条件相同的操作:If
x
is a slice, you can do the same as the other condition:x.step 或 1
怎么样?how about
x.step or 1
?