2 Star 1 Fork 15

jackfrued/review

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example08.py 655 Bytes
一键复制 编辑 原始数据 按行查看 历史
jackfrued 提交于 2020-06-06 12:07 . 异步编程和协作式并发
"""
迭代器:实现了迭代协议的对象。
~ 魔术方法:__xxx__
~ __next__ / __iter__
上下文管理器:实现了上下文管理器协议的对象
~ __enter__ / __exit__
"""
# 1 1 2 3 5 8 13 21 34 55
class FibIter:
def __init__(self, num):
self.num = num
self.a, self.b = 0, 1
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.count < self.num:
self.a, self.b = self.b, self.a + self.b
self.count += 1
return self.a
raise StopIteration()
for value in FibIter(20):
print(value)
print(4181 / 6765)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jackfrued/review.git
git@gitee.com:jackfrued/review.git
jackfrued
review
review
master

搜索帮助