美文网首页
Python3 中的切片魔术方法

Python3 中的切片魔术方法

作者: 木子识时务 | 来源:发表于2018-12-31 18:25 被阅读0次

从 Python3 的官方文档可知,Python3中的切片的魔术方法不再是Python2中的__getslice__(), __setslice__()__delslice__(),而是借助 slice 类整合到了__getitem__(),__setitem__()__delitem__()中。
具体如下:

  • __getslice__(), __setslice__() and __delslice__() were killed. The syntax a[i:j] now translates toa.__getitem__(slice(i, j)) (or __setitem__() or __delitem__(), when used as an assignment or deletion target, respectively).

Python2 中的切片魔术方法

Python2 中切片魔术方法的使用示例如下:

#-*- coding:utf-8 -*-
#File Name:slice_operator_python2.py
#Created Time:2018-12-31 18:15:56

class Foo(object):

    def __getslice__(self, i, j):
        print('__getslice__', i, j)

    def __setslice__(self, i, j, sequence):
        print('__setslice__', i, j)

    def __delslice__(self, i, j):
        print('__delslice__', i, j)


if __name__ == "__main__":
    obj = Foo()

    obj[-1:1]                   # 自动触发执行 __getslice__
    obj[0:1] = [11,22,33,44]    # 自动触发执行 __setslice__
    del obj[0:2]   

运行结果如下:


python2 中运行结果

如果换用Python3,则会提示如下错误:


Python3运行结果

Python3 中的切片魔术方法

#-*- coding:utf-8 -*-
#File Name:slice_operator.py
#Created Time:2018-12-31 17:50:31


class Foo(object):

    def __getitem__(self, index):
        if isinstance(index, slice):
            print("Get slice---------> start: %s, stop: %s, step: %s." \
                    % (str(index.start), str(index.stop), str(index.step)))

    def __setitem__(self, index, value):
        if isinstance(index, slice):
            print("Set slice---------> start: %s, stop: %s, step: %s." \
                    % (str(index.start), str(index.stop), str(index.step)))
            print("\tThe value is:", value)
        
    def __delitem__(self, index):
        if isinstance(index, slice):
            print("Delete slice------> start: %s, stop: %s, step: %s." \
                    % (str(index.start), str(index.stop), str(index.step)))


if __name__ == "__main__":
    obj = Foo()
    obj[-1:10]
    obj[-1:10:1] = [2,3,4,5]
    del obj[-1:10:2]

python3 中的运行结果:


Python3中运行结果

相关文章

  • Python3 中的切片魔术方法

    从 Python3 的官方文档可知,Python3中的切片的魔术方法不再是Python2中的__getslice_...

  • PHP魔术方法

    魔术方法(Magic methods) PHP中把以两个下划线__开头的方法称为魔术方法,这些方法在PHP中充当了...

  • Python面向对象的魔术方法

    魔术方法 查看类的魔术方法 输出结果如下 在Python中,所有以__双下划线包起来的方法,都统称为魔术方法。比如...

  • 魔术方法及魔术常量详解

    魔术方法 魔术方法中充当了举足轻重的作用,大部分魔术方法起着报错补救措施的作用 __construct()类的构造...

  • Python3序列赋值、序列解包详解(下)

    扩展序列解包 上节我们提到解决赋值中等号两边参数不一致的方法可以通过切片,但在Python3中我们可以利用特定的语...

  • Python学习打call第三十天:魔术方法

    1.什么是魔术方法 在Python中以两个下划线开头和结尾的方法被称为魔术方法,魔术方法都是一些内置方法; 2.基...

  • Python魔术方法

    Python魔术方法 Python 魔术方法指南 — PyCoder’s Weelky CNPython的魔术方法...

  • PHP中的魔术常量、魔术方法

    魔术常量:__CLASS__和__METHOD__ __CLASS__:获取其所在的类的类名__METHOD__:...

  • PHP反序列化总结

    问题原因:漏洞的根源在于unserialize()函数的参数可控。如果反序列化对象中存在魔术方法,而且魔术方法中的...

  • Python 切片与迭代

    切片 切片是 python 中截取列表、元组或字符串中部分元素的快捷方法,使用 [] 来实现。 切片的完整写法 ...

网友评论

      本文标题:Python3 中的切片魔术方法

      本文链接:https://www.haomeiwen.com/subject/nrjwlqtx.html