为什么在Python 3.5中调用__getitem__而在Python 2.7中调用?

Ree*_*ece 1 python python-2.7 python-3.x python-internals python-3.5

我有一个字节的子类,提供了一个__getitem__dunder方法.该__getitem__方法始终在Python 3.5中调用,但仅在Python 2.7中调用非切片键.(相反,似乎父项__getitem__已应用于实例.)为什么这样,是否有解决方法?

class A(object):
    def __getitem__(self, key):
        print("in A.__getitem__ with key " + str(key))
        return []

class B(bytes):
    def __getitem__(self, key):
        print("in B.__getitem__ with key " + str(key))
        return []

if __name__ == "__main__":
    import sys
    print(sys.version)

    a = A()
    b = B()

    print("[0]")
    a[0]
    b[0]

    print("[0:1]")
    a[0:1]
    b[0:1]

    print("[:1]")
    a[:1]
    b[:1]
Run Code Online (Sandbox Code Playgroud)

Python 3.5输出

__getitem__始终调用类定义.

(venv) snafu$ python ./x.py
3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609]
[0]
in A.__getitem__ with key 0
in B.__getitem__ with key 0
[0:1]
in A.__getitem__ with key slice(0, 1, None)
in B.__getitem__ with key slice(0, 1, None)
[:1]
in A.__getitem__ with key slice(None, 1, None)
in B.__getitem__ with key slice(None, 1, None)
Run Code Online (Sandbox Code Playgroud)

Python 2.7输出

__getitem__仅为非切片键调用类定义.

(venv2.7) snafu$ python x.py
2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
[0]
in A.__getitem__ with key 0
in B.__getitem__ with key 0
[0:1]
in A.__getitem__ with key slice(0, 1, None)
[:1]
in A.__getitem__ with key slice(None, 1, None)
Run Code Online (Sandbox Code Playgroud)

use*_*ica 6

Python 2具有__getslice__优先级,__getitem__如果存在,则优先于无级切片.这在Python 3中已经消失了.