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__改爲生成器函數spa
def __iter__(self): for word in self.words: yield word #yield爲生成器關鍵字
改爲生成器後用法不變,但更加簡潔。code
3.惰性實現blog
當列表比較大,佔內存較大時,咱們能夠採用惰性實現,每次只讀取一個元素到內存。內存
def __iter__(self): for match in RE_WORD.finditer(self.text): yield match.group()
或者使用更簡潔的生成器表達式get
def __iter__(self): return (match.group() for match in RE_WORD.finditer(self.text))
4.yield fromit
itertools模塊含有大量生成器函數可供利用io
def chain(*iterables): for it in iterables: for i in it: yield i
等價於class
def chain(*iterables): for it in iterables: yield from it