class Fibonacci(object):
def __init__(self, all_num):
self.all_num = all_num
self.current_num = 0
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
if self.current_num < self.all_num: # 判断是否可以迭代
ret = self.a
self.a, self.b = self.b, self.a + self.b
self.current_num += 1
return ret
else:
raise StopIteration # 迭代完成,抛出异常,退出迭代
fib = Fibonacci(10)
for num in fib:
print(num)
# 迭代器的优点,占用极小的空间去迭代