不同版本的Python uu getitem uuu行为

不同版本的Python uu getitem uuu行为,python,python-2.7,magic-methods,Python,Python 2.7,Magic Methods,Windows上python 2.7.3、2.7.7、2.7.8和3.3.2的输出: class A(object): def __getitem__(self, item): print item a = A() a[0:-1] win 32上python 2.7.6(32位)的输出: slice(0, -1, None) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 AttributeError:实例没有属性“\uuu len\uuu” 在Python

Windows上python 2.7.3、2.7.7、2.7.8和3.3.2的输出:

class A(object):
    def __getitem__(self, item):
        print item

a = A()
a[0:-1]
win 32上python 2.7.6(32位)的输出:

slice(0, -1, None)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:实例没有属性“\uuu len\uuu”
在Python2.7.6中,slice对象尝试获取实例的长度,以便将“-1”转换为实际值。例如,如果实例的长度为10,则输出将为“slice(0,9,None)”


这种行为似乎很奇怪。有人能证实我的观察是否正确吗?如果是,那么这背后是否有任何官方文件?我们如何应对这种行为以支持我们在所有版本的python上的项目?

看起来像是一个在2.7.3和2.7.8之间存在的bug

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute '__len__'

Python 2.7.8 (default, Jul  1 2014, 17:30:21) 
[GCC 4.9.0 20140604 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...     def __getitem__(self, item):
...         print item
... 
>>> a = A()
>>> a[0:-1]
slice(0, -1, None)

它可能在Windows上。在Windows上尝试2.7.8,看看你是否可以在那里复制它。

如果是这样,那么它看起来像是一个bug(他们可能在python2.7.8中修复了…)在Windows上适用于python2.7.4(32位)。正如我在问题中提到的,它在Windows上的2.7.8上产生相同的结果。唯一的异常是Python2.7.6
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...     def __getitem__(self, item):
...         print item
... 
>>> a = A()
>>> a[0:-1]
slice(0, -1, None)