# /usr/bin/python3
# —*— encoding:utf-8 —*—
# 使用迭代器生成斐波那契数列
class Fibonacci(object):
def __init__(self, x):
self.x = x
self.num1 = self.num2 = 1
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.count < self.x:
z = self.num1
self.num1, self.num2 = self.num2, self.num2 + self.num1
self.count += 1
return z
else:
raise StopIteration
for i in Fibonacci(12):
print(i)
Python 使用迭代器生成斐波那契数列
最新推荐文章于 2022-04-11 15:07:40 发布