What does this print?
def inner():
yield 1
yield 2
def outer():
yield 0
yield from inner()
yield 3
print(list(outer()))Options
- [0, <generator>, 3]
- [0, 1, 2, 3] (Correct option)
- [0, 3]
- [0, [1, 2], 3]
Answer
[0, 1, 2, 3]
Why
yield from inner() passes along every value that inner() produces, as if outer() yielded them itself.
So we get 0, then 1 and 2 from inner, then 3.
Remember
yield from = hand over to another generator.