In the following block of code:
def bottom():
# Returning the yield lets the value that goes up the call stack to come right back down.
return (yield 42)
def middle():
return (yield from bottom())
def top():
return (yield from middle())
# Get the generator.
gen = top()
value = next(gen)
print(value) # Prints '42'.
try:
value = gen.send(value*2)
except StopIteration as exc:
value = exc.value
print(value) # Prints '84'.
- What does return (yield 42) actually do ? Why not simply return 42 and why is (yield 42) in parenthesis ?
- What does he mean by this: "Returning the yield lets the value that goes up the call stack to come right back" ?
- Why is he using "yield from" in the "top" and "middle" functions ?