Python迭代器与生成器详解(4)

1 class Fib(object): 2 def __init__(self, limit): 3 self.a, self.b = 0, 1 4 self.limit = limit 5 6 def __iter__(self): 7 return self 8 9 def __next__(self): 10 self.a, self.b = self.b, self.a+self.b 11 while self.a > self.limit: 12 raise StopIteration 13 return self.a 14 15 for n in Fib(1000): 16 print(n)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e6a0c31b6f1b387c60317b92283f0191.html