python 生成器

import sys

L = [11,2,25,7]
	
def OutList():
	for i in L:
		print("before yield")
		yield i#函数中存在yield关键字,当调用该函数时,函数内部不做停留处理,立刻返回迭代器
		print("end yield")
		
	
f = OutList()#该函数调用不做停留,仅仅返回迭代器

while True:
	try:
		print("before out L")
		print(next(f))
		print("end out L")
	except StopIteration:
		print("Out Of Range")
		sys.exit()

 

执行结果如下:

 

posted @ 2019-07-19 17:16  突突兔  阅读(115)  评论(0编辑  收藏  举报