1.经典迭代器
import re RE_WORD = re.compile('\w+') class Sentence: def __init__(self, text): self.text = text self.words = RE_WORD.findall(text) def __iter__(self): # <1> return SentenceIterator(self.words) # <2> class SentenceIterator: def __init__(self, words): self.words = words # <3> self.index = 0 # <4> def __next__(self): try: word = self.words[self.index] # <5> except IndexError: raise StopIteration() # <6> self.index += 1 # <7> return word # <8> def __iter__(self): # <9> return self def main(): s = Sentence('hello all') for word in s: #隐式调用iter(s),加入s存在__iter__则调用返回迭代器,否则若s存在__getitem__,则默认生成迭代器,调用__getitem__生成元素 print(word) if __name__ == '__main__': main()
2.将Sentence中的__iter__改成生成器函数
def __iter__(self): for word in self.words: yield word #yield为生成器关键字
改成生成器后用法不变,但更加简洁。
3.惰性实现
当列表比较大,占内存较大时,我们可以采用惰性实现,每次只读取一个元素到内存。
def __iter__(self): for match in RE_WORD.finditer(self.text): yield match.group()
或者使用更简洁的生成器表达式
def __iter__(self): return (match.group() for match in RE_WORD.finditer(self.text))
4.yield from
itertools模块含有大量生成器函数可供利用
def chain(*iterables): for it in iterables: for i in it: yield i
等价于
def chain(*iterables): for it in iterables: yield from it