数学中有个著名的斐波拉契数列(Fibonacci),数列中第一个数为0,第二个数为1,其后的每一个数都可由前两个数相加得到:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
现在我们来用迭代器实现一下
class Fibonacci(object):
def __init__(self, n):
# n 表示计算前n项 用以判断迭代的条件
self.n = n
# current 用来记录访问的位置
self.current = 0
self.num1 = 1
self.num2 = 2
def __iter__(self):