def myYield2(): for i in range(3): yield '2222 i am in myYield2', 'i = ', i def 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 = ', j f = myYield() print 'KKK', f.next…
网络上介绍yield的文章很多,但大多讲得过于复杂或者追求全面以至于反而不好理解.本文用一个极简的例子给出参考资料[1]中的讲解,因为个人觉得其讲解最为通俗易懂,读者只需要对Python的列表有所了解即可. When you see a function with yield statements, apply this easy trick to understand what will happen: 1. Insert a line result = [] at the start of…