GAP·MAP
← all breakdowns
GeneratorsIteratorsLazy Evaluationjunior level

How Python's yield pauses and resumes work

the question

What does the `yield` keyword do in Python, and how is it different from `return`?

what they're testing: The interviewer is checking whether you understand lazy iteration and the execution lifecycle of a generator.

a strong answer

In a regular def, yield makes the function a generator function. Calling it returns a generator object without running the body. When a for loop or next() requests a value, the body runs to the next yield, passes that value to the caller, and pauses there.

The pause is the useful part. Python keeps the generator's local variables and current position, so the next request continues just after the previous yield. This produces values on demand instead of building a whole list first. It works well for large inputs, streams, pipelines, and infinite series. The trade-off is that a generator is a single-pass iterator. Once exhausted, it does not rewind, so you call the generator function again to make a fresh one.

return ends execution of the function, while yield suspends execution so it can continue later. During iteration, executing return or reaching the end exhausts the generator and raises StopIteration. A for loop handles that exception. yield from iterable delegates to another iterable and passes its values to the outer caller. For advanced use, send() can resume a suspended generator with a value, while ordinary iteration resumes it with None.

Where people slip

the tempting wrong answer, and what's actually true

  • A function containing `yield` returns a list of all the yielded values.

    Calling it returns a generator iterator that produces values as iteration requests them.

  • Calling a generator function runs it immediately up to the first `yield`.

    Calling it only creates the generator; execution begins when iteration calls `next()` or another generator method.

  • A generator can be rewound and iterated again after it is exhausted.

    A generator is single-pass, so another call to the generator function is needed for a fresh iterator.

  • `yield` is just `return` with lower memory usage.

    `yield` suspends execution while preserving local state, whereas `return` ends execution and signals that the generator is finished.

If they push further

When would you choose a generator over returning a list?

Choose a generator when values can be consumed incrementally, especially for large, streaming, or unbounded inputs. A list is better when callers need indexing, repeated passes, or all results at once.

What happens when a generator finishes?

During iteration, reaching the end or executing `return` raises `StopIteration` to the consumer; a `for` loop handles it and ends normally.

What does `yield from` do?

It delegates iteration to another iterable, forwarding its produced values to the outer generator's caller.

Sources

Now answer it yourself.

Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.

was this useful?

beta

The interviewer part is in the works.

The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.