So, here is a python expression:
a = yield from f()
What does it mean? Where can it be used? What kind of object should f
be? What will be the value of a
after the expression is evaluated?
There are several questions here on stackoverflow about python's yield
and yield from
but I did not find an answer to this.
I understand the meaning of yield x
, y = yield
and y = yield x
. Even yield from f()
is more or less understandable. But a = yield from f()
is something unexpected for me.
UPDATE:
B. Barbieri provided correct answer. Still I need to formulate it a little bit differently.
Semantic of the expression a = yield from f()
is very similar to a function call: value of a
would be what f()
returns. But if f()
yields anything, the yielded value would be forwarded to "upper level" (you can only write a = yield from f()
inside a function, and this will make you function a generator). If after that the "upper level" sends a value back to your generator, the value will be forwarded to f()
and the f()
will continue.
The yield from
allows f()
and "upper level" to communicate while your function is running.
I guess now I do understand what this yield from
is all about and hope this explanation would be helpful for others.