我正在遵循Fluent Python一书的第14章控制流程,
使用以下代码遇到麻烦:
class ArithmeticProgression:
def __init__(self, begin, step, end=None):
self.begin = begin
self.step = step
self.end = end
def __iter__(self):
result = type(self.begin + self.step)(self.begin)
forever = self.end is None
index = 0
while forever or result < self.end:
yield result
index = + 1
result = self.begin + self.step * index
当我根据书进行测试时:
In [220]: ap = ArithmeticProgression(1, 1, 5)
In [221]: ap
Out[221]: <__main__.ArithmeticProgression at 0x1098b2160>
In [222]: list(ap)
#it run endless not return any result
#I should Ctrl+C to break it mandatory
我用源代码example-code/aritprog_v1.py at master · fluentpython/example-code逐行检查我的代码
但找不到任何问题。
答案 0 :(得分:4)
v-bind:placeholder="placeholder"
应该是
index = + 1
每次迭代将为index += 1
加一个。照原样,这是index
或index = +1
。因此index = 1
的值在循环之间永远不会改变。