Python · The language, not just syntax

How do generators work and why use them?

MediumgeneratorslazinessyieldUpdated Oct 2026
Quiz this track

Short answer

A generator is a function containing yield. Calling it returns an iterator that produces values lazily, one at a time, preserving local state between resumptions and avoiding building the whole result in memory.

When a generator function is called, its body does not run. It returns a generator object. Each next call runs until the next yield, pauses, and resumes from the same point on the following call. When the function returns, the generator raises StopIteration.

python
def read_rows(path):
    with open(path, encoding="utf-8") as handle:
        for line in handle:
            yield line.rstrip("\n")

def non_empty(rows):
    for row in rows:
        if row:
            yield row

rows = non_empty(read_rows("data.txt"))
for row in rows:
    print(row)
The pipeline processes one line at a time, so memory stays proportional to a single row rather than the file.

What generators give you

  • Lazy evaluation: work happens only when a value is requested
  • Constant memory for streaming large files, logs or API pages
  • Composable pipelines where each stage yields to the next
  • Infinite sequences that are consumed on demand

Generators are one-shot. Once exhausted, iterating again yields nothing, so you must recreate the generator or materialise it into a list if multiple passes are needed. They also support send, throw and close, which enables coroutine-style code, though asyncio is usually clearer for concurrency.

Generators are not automatically faster. They add per-item overhead. Use them for memory and streaming benefits, not as a blanket performance optimisation.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What happens if you iterate the same generator twice?
  2. 2How does yield differ from return?
  3. 3What are send and close used for?
  4. 4When is a generator a worse choice than a list?

Say it in the interview

A generator is a function with yield. Calling it returns an iterator that runs lazily and keeps its local state between resumptions. I use generators for streaming files, paginated API results and pipeline stages where memory would otherwise grow with input size. The important caveat is that they are one-shot, so if I need multiple passes I materialise the data deliberately.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.