Python · The language, not just syntax
How do generators work and why use them?
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.
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)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.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What happens if you iterate the same generator twice?
- 2How does yield differ from return?
- 3What are send and close used for?
- 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.