I understand what the yield
keyword does. But here I found an example for yield
usage which makes me frustrated:
@defer.inlineCallbacks
def doStuff():
result = yield takesTwoSeconds()
nextResult = yield takesTenSeconds(result * 10)
defer.returnValue(nextResult / 10)
That is a Twisted
example. Here is (as author describes) yield
used for async work or something like that. I decided to test it with non-async simple code:
import random
def func():
return random.randint(0, 10)
def foo():
while True:
x = yield func()
print("'x' value is", x)
f = foo()
for i in range(0, 3):
print(next(f))
and I get as output:
6
'x' value is None
0
'x' value is None
7
So why do I get None
for yield/return
function value while example by author (mentioned above) gets proper value and insert it at next expressions?