1.4 理解Python中使用yield进行迭代
def yiled_test(n):
for i in range(n):
yield i*2
# 下一次迭代时,从下面的print('i=',i)开始执行
print('i in the yield_test',i)
print('Finish at the end of yiled_test')
if __name__ == '__main__':
# 必须使用迭代来获取yield_test()函数返回值
for i in yiled_test(5):
print('i in the main',i)Last updated