Discuss / Python / 切片初始索引采用默认的问题

切片初始索引采用默认的问题

Topic source

小酷库

#1 Created at ... [Delete] [Delete and Lock User]

class Fib(object): def getitem(self, n): if isinstance(n, int): # n是索引 a, b = 1, 1 for x in range(n): a, b = b, a + b return a if isinstance(n, slice): # n是切片 start = n.start stop = n.stop a, b = 1, 1 L = [] for x in range(stop): if x >= start: L.append(a) a, b = b, a + b return L

f[:10] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

start的值为None.

熊巍迤

#2 Created at ... [Delete] [Delete and Lock User]

我和你一样f[:10] start为None而不是0.然后就会报错。python版本是3.4.3


  • 1

Reply