Repaso
EspaƱol
PythonIntermediatePY-29

What does this print?

def inner():
    yield 1
    yield 2

def outer():
    yield 0
    yield from inner()
    yield 3

print(list(outer()))

Options

  1. [0, <generator>, 3]
  2. [0, 1, 2, 3] (Correct option)
  3. [0, 3]
  4. [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.

Key terms

Practice this question in Repaso