目录

* 一、简单示例 <https://www.cnblogs.com/nickchen121/p/10991493.html#一简单示例>
* 二、StopIteration异常版
<https://www.cnblogs.com/nickchen121/p/10991493.html#二stopiteration异常版>
* 三、模拟range <https://www.cnblogs.com/nickchen121/p/10991493.html#三模拟range>
* 四、斐波那契数列 <https://www.cnblogs.com/nickchen121/p/10991493.html#四斐波那契数列>
一、简单示例

* 死循环 class Foo: def __init__(self, x): self.x = x def __iter__(self): return
self def __next__(self): self.x += 1 return self.x f = Foo(3) for i in f:
print(i)
二、StopIteration异常版

* 加上StopIteration异常 class Foo: def __init__(self, start, stop): self.num =
start self.stop = stop def __iter__(self): return self def __next__(self): if
self.num >= self.stop: raise StopIteration n = self.num self.num += 1 return n
f = Foo(1, 5) from collections import Iterable, Iterator print(isinstance(f,
Iterator)) True for i in Foo(1, 5): print(i) 1 2 3 4
三、模拟range
class Range: def __init__(self, n, stop, step): self.n = n self.stop = stop
self.step = step def __next__(self): if self.n >= self.stop: raise
StopIteration x = self.n self.n += self.step return x def __iter__(self):
return self for i in Range(1, 7, 3): print(i) 1 4
四、斐波那契数列
class Fib: def __init__(self): self._a = 0 self._b = 1 def __iter__(self):
return self def __next__(self): self._a, self._b = self._b, self._a + self._b
return self._a f1 = Fib() for i in f1: if i > 100: break print('%s ' % i,
end='') 1 1 2 3 5 8 13 21 34 55 89

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信