0

Let's say that exists a function as such:

def subroutine():
    While True:    
        value = yield
        yield value**2

For a fact, I know that the output is a generator that squares the input, and this subroutine is run after other generator(s) have been priorly run. However, I don't understand how value = yield actually gets its value.

  • It's extending from mere generation of values to a true coroutine. Read the section on coroutines on [this answer](https://stackoverflow.com/a/31042491/364696). The duplicate is technically asking the question from the other way around (they found `send`, which is how you use a generator of the form you're asking about), but it answers your question completely. – ShadowRanger Jun 14 '20 at 20:21
  • Now seems clear to me. – Ricardo Barros Lourenço Jun 15 '20 at 01:11

0 Answers0