摘要:
生成器是一种特殊的迭代器。 用生成器实现斐波那契数列: from collections import Iterator, Iterable def create_nums(num): a, b = 1, 1 cnt = 0 while cnt < num: yield a a, b = b, a 阅读全文
摘要:
from collections import Iterable, Iterator class ClassIterator: """迭代器必须实现__iter__和__next__方法,故迭代器一定是可迭代对象""" def __init__(self, obj): self.obj = obj 阅读全文