首先很感谢作者写代码给我的思路,其实改一改代码就更加的明白了
def genter():
a = 4
b = 5
c = 6
for i in range(5):
yield a
print('a was print at '+ str(i))
yield b
print('b was print at '+ str(i))
yield c
res = genter()
for i, c in enumerate(res):
print('this is '+str(i)+' steps')
if i>1:
break
print(c)
这是运行的结果:
this is 0 steps
4
a was print at0
this is 1 steps
5
b was print at 0
this is 2 steps
Python Yield 精髓对 Python 中的 Yield 一直理解的不够深刻,甚至存在误解。遇到一个神奇的用法后(多个 yield 连续使用)又好好研究了下,以下记录鄙人粗糙见解。 首先简单科普一...