Python yield, pythonyield
I saw the yield keyword in other people's code, read some documents, and explain my understanding.
A function that contains the yield keyword becomes an iterator. yield is at the same position as return, but does not exit after each result is returned, but waits for the next iteration, after the next iteration, execute the statement after yield until yield is encountered again, and a new result is returned.
Of course, those who are not familiar with yield may not understand the iterator (for example, I ^), so in a more general sense, if a function (SCRIPT) if repeated (loop) execution is required and the result of each loop is required, replace return with yield, for example:
>>> Def fib (max ):... n, a, B = 0, 0, 1... while n <max :... yield... a, B = B, a + B... n + = 1 >>>> f1.next () 0 >>> f1.next () 1 >>> f1.next () 1 >>>> f1.next () 2 >>> f1.next () 3 >>> f1.next () Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration