See*_*Yre 5 python magic-methods python-2.7
class A(object):
def __getitem__(self, item):
print item
a = A()
a[0:-1]
Run Code Online (Sandbox Code Playgroud)
Windows上的python 2.7.3,2.7.7,2.7.8,3.3.2上的输出:
slice(0, -1, None)
Run Code Online (Sandbox Code Playgroud)
win 32上的python 2.7.6(32位)输出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute '__len__'
Run Code Online (Sandbox Code Playgroud)
在python 2.7.6中,切片对象尝试获取实例的长度,以便它可以将"-1"转换为实际值.例如,如果实例的长度为10,则输出将为"slice(0,9,None)".
这种行为似乎很奇怪.任何人都可以验证我的观察是否正确吗?如果是,那么这背后是否有任何官方文件?我们如何应对这种行为以支持我们在所有版本的python上的项目?
看起来 2.7.3 和 2.7.8 之间存在一个错误
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)
Run Code Online (Sandbox Code Playgroud)
相对
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)
Run Code Online (Sandbox Code Playgroud)
它可能在 Windows 上。去 Windows 上尝试 2.7.8,看看是否可以重现这个问题。
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |