首先很感謝作者寫代碼給我的思路,其實改一改代碼就更加的明白了
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 連續使用)又好好研究了下,以下記錄鄙人粗糙見解。 首先簡單科普一...