标签:yield
yield生成斐波那契:#!/usr/bin/env python #-*- coding=utf-8 -*- def fab(max): n,a,b = 0,0,1 while n < max: yield b a , b = b,a+b n += 1 if __name__ == "__main__": for i in fab(15): print i
yield读大文件内容:
#!/usr/bin/env python #-*- coding=utf-8 -*- def read_file(): block_size = 1024 with open("/root/code/log/a.txt","r") as f: while True: block = f.read(block_size) if block: yield block else: return if __name__=="__main__": for i in read_file(): print i
标签:yield
原文地址:http://blog.51cto.com/guoshiwei/2124820