标签:stop __iter__ amp 一个 ret += top class ISE
#斐波那契数列(F(N-1)/F(N)->0.618) & F(N)=F(N-2)+F(N-1)
class Fibonacci:
def __init__(self, n):
self.__count = n
def __iter__(self):
return FiboIterator(self.__count)
class FiboIterator:
def __init__(self,n):
self.__count = n
self.cur_count = 0
self.a =0 #用来保存前第二个数
self.b =1 #用来保存前一个数
def __next__(self):
if self.cur_count>=self.__count:
raise StopIteration
self.cur_count += 1
self.a, self.b = self.b, self.a + self.b
return self.a
for x in Fibonacci(10):
print(x)
L = [x for x in Fibonacci(30)]
print(L)
print(sum(Fibonacci(25)))
斐波那契数列的存储与统计
标签:stop __iter__ amp 一个 ret += top class ISE
原文地址:https://www.cnblogs.com/python-book/p/10852762.html