我在 Python (2.7.4) 中玩切片:
class Foo():
def __getitem__(self, key):
# Single value
if isinstance(key, (int,long)):
return key
# Slice
if isinstance(key, slice):
print 'key.start = 0x{0:X} key.stop = 0x{1:X}'.format(key.start, key.stop)
length = key.stop - key.start
return str(length)
一切似乎都按预期工作:
>>> f = Foo()
>>>
>>> f[42]
42
>>>
>>> f[20:30]
key.start = 0x14 key.stop = 0x1E
'10'
除了切片索引似乎限制为 0x7FFFFFFF 之外:
>>> f[0xFEDCBA98 : 0xFFFFFFFF]
key.start = 0x7FFFFFFF key.stop = 0x7FFFFFFF
'0'
>>> f[0x80000000:0x90000000]
key.start = 0x7FFFFFFF key.stop = 0x7FFFFFFF
'0'
long
为什么切片索引不像常规值那样受到相同的整数提升int
?有什么解决方法吗?