basic understanding about yield

This question already has an answer here:

  • What does the “yield” keyword do? 36 answers

  • yield returns the control to the caller scope (function or method), as would return - but instead of destroying the current scope, with the local variables and its values, it is kept.

    When the "next" method of the iterator is called again (which the for statement does implicitly), the scope where the yield was executed is retrieved, and execution continue from that point on, with the same variables in place.

    Actually, yield behaves as an expression, and if instead of calling the next method, the outer function calls send instead, the value passed to send is the value returned by the yield expression.

    (This information about the code object, local and global variables, which line is being run is kept in a "stack frame" object, and in Python it can be even addressed as any other object and introspected)

    链接地址: http://www.djcxy.com/p/1554.html

    上一篇: 不明白这种方式的作品

    下一篇: 对产量的基本理解