python切片的对象_Python切片对象和__

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}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值