Can someone give me an example of why the "send" function associated with Python generator function exists? I fully understand the yield function. However, the send function is confusing to me. The documentation on this method is convoluted:
What does that mean? I thought value was the input to the function? The phrase "The send() method returns the next value yielded by the generator" seems to be also the exact purpose of the yield function; yield returns the next value yielded by the generator... Can someone give me an example of a generator utilizing send that accomplishes something yield cannot?
It's used to send values into a generator that just yielded. Here is an artificial (non-useful) explanatory example. Consider a testing function:
As to why it's useful, one of the best use cases I've seen is Twisted's @defer.inlineCallbacks. Essentially it allows you to write a function like this:
* Python generator.send with yield