码迷,mamicode.com
首页 > 其他好文 > 详细

迭代器实现斐波那契数列

时间:2019-01-22 20:42:01      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:return   __init__   color   nbsp   __iter__   author   port   迭代器   init   

 1 #Author : Kelvin
 2 #Date : 2019/1/22 20:02
 3 class Fi:
 4     def __init__(self):
 5         self.a=1
 6         self.b=1
 7     def __iter__(self):
 8         return self
 9     def __next__(self):
10         if self.b>100:
11             raise StopIteration("out of range")
12         self.a,self.b=self.b,self.a+self.b
13         return self.a
14 import time
15 f=Fi()
16 for i in f:
17     print(i)
18     time.sleep(1)

 

迭代器实现斐波那契数列

标签:return   __init__   color   nbsp   __iter__   author   port   迭代器   init   

原文地址:https://www.cnblogs.com/sun-10387834/p/10305855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!