我正在创建一个表示列表列表的类。__让我头疼。一切进展顺利,直到我引入切片作为参数。在
演示代码# Python version 2.7.5
class NestedLists:
_Cells = [['.', '.', '.', '.', '.'],
['.', '.', 'N', '.', '.'],
['.', 'C', 'A', 'T', '.'],
['.', '.', 'P', '.', '.'],
['.', '.', '.', '.', '.']]
def __getitem__(self, index):
if isinstance(index, int):
return self._Cells[index]
elif isinstance(index, slice):
return self._Cells[index]
else:
raise TypeError, "Invalid argument type"
nested = NestedLists()
print "Expecting A"
print nested[2][2]
print "Expecting CAT"
print nested[2][1:4]
print "Expecting ..N.."
print " .CAT."
print " ..P.."
print nested[1:4]
print "Expecting .N."
print " CAT"
print " .P."
print nested[1:4][1:4]
以下输出
^{pr2}$
显然,发生的是第二个[]运算符调用被应用于第一个。。。但还是停留在最外面的名单上。然而,我无法找到解决办法。在