摘要: 生成器是一种特殊的迭代器。 用生成器实现斐波那契数列: from collections import Iterator, Iterable def create_nums(num): a, b = 1, 1 cnt = 0 while cnt < num: yield a a, b = b, a 阅读全文
posted @ 2020-03-31 10:45 olivertian 阅读(106) 评论(0) 推荐(0) 编辑
摘要: from collections import Iterable, Iterator class ClassIterator: """迭代器必须实现__iter__和__next__方法,故迭代器一定是可迭代对象""" def __init__(self, obj): self.obj = obj 阅读全文
posted @ 2020-03-31 00:04 olivertian 阅读(246) 评论(0) 推荐(0) 编辑