I was going to post "read page 19 of Beazley's 'Python: Essential Reference' for a quick description of generators", but so many others have posted good descriptions already.
Also, note that yield
can be used in coroutines as the dual of their use in generator functions. Although it isn't the same use as your code snippet, (yield)
can be used as an expression in a function. When a caller sends a value to the method using the send()
method, then the coroutine will execute until the next (yield)
statement is encountered.
Generators and coroutines are a cool way to set up data-flow type applications. I thought it would be worthwhile knowing about the other use of the yield
statement in functions.