class FibIterator(object):
"""斐波那契数列迭代器"""
def __init__(self, n):
"""
:param n: int, 指明生成数列的前n个数
"""
self.n = n
# current用来保存当前生成到数列中的第几个数了
self.current = 0
# num1用来保存前前一个数,初始值为数列中的第一个数0
self.num1 = 0
# num2用来保存前一个数,初始值为数列中的第二个数1
self.num2 = 1
def __next__(self):
"""被next()函数调用来获取下一个数"""
if self.current < self.n:
num = self.num1
self.num1, self.num2 = self.num2, self.num1+self.num2
self.current += 1
return num
else:
raise StopIteration
def __iter__(self):
"""迭代器的__iter__返回自身即可"""
return self
if __name__ == ‘__main__‘:
fib = FibIterator(10)
for num in fib:
print(num, end=" ")
http://mseo.chinaz.com/lvyoup1.nx04.com/ http://seo.chinaz.com/lvyoup2.nx04.com/ http://mseo.chinaz.com/lvyoup3.nx04.com/ http://seo.chinaz.com/lvyoup4.nx04.com/
http://mseo.chinaz.com/lvyou5.nx04.com/ http://seo.chinaz.com/lvyoup6.nx04.com/ http://mseo.chinaz.com/lvyoup7.nx04.com/ http://seo.chinaz.com/lvyoup8.nx04.com/