標籤:
def myYield2(): for i in range(3): yield ‘2222 i am in myYield2‘, ‘i = ‘, idef myYield(): for i in range(10): yield ‘0000 i am in myYield‘, ‘i = ‘, i for j in myYield2(): yield ‘1111 i am in for myYield2‘, ‘j = ‘, jf = myYield()print ‘KKK‘, f.next()print ‘KKK‘, f.next()print ‘KKK‘, f.next()print ‘KKK‘, f.next()print ‘KKK‘, f.next()print ‘KKK‘, f.next()print ‘KKK‘, f.next()
yield有一點像return, 返回的結果如下:
KKK (‘0000 i am in myYield‘, ‘i = ‘, 0)KKK (‘1111 i am in for myYield2‘, ‘j = ‘, (‘2222 i am in myYield2‘, ‘i = ‘, 0))KKK (‘1111 i am in for myYield2‘, ‘j = ‘, (‘2222 i am in myYield2‘, ‘i = ‘, 1))KKK (‘1111 i am in for myYield2‘, ‘j = ‘, (‘2222 i am in myYield2‘, ‘i = ‘, 2))KKK (‘0000 i am in myYield‘, ‘i = ‘, 1)KKK (‘1111 i am in for myYield2‘, ‘j = ‘, (‘2222 i am in myYield2‘, ‘i = ‘, 0))KKK (‘1111 i am in for myYield2‘, ‘j = ‘, (‘2222 i am in myYield2‘, ‘i = ‘, 1))
python學習 - yield