Iterators & Generators
Generators
Generator functions using yield — lazy evaluation, memory efficiency, sending values into generators, and generator lifecycle management.
Interview: Generators are a top interview topic — they demonstrate understanding of lazy evaluation, memory efficiency, and Pythonic design.
A generator is a function that uses yield instead of return to produce a sequence of values lazily. Each call to next() resumes the function from where it last yielded, preserving all local state. Generators are the most Pythonic way to create iterators.
How Generators Work
- Calling a generator function returns a generator object (doesn't execute the body)
next(gen)runs the function until the firstyield, returns that value- Subsequent
next()calls resume from the last yield point - When the function returns (or reaches the end),
StopIterationis raised
Memory Efficiency
- Generators produce one value at a time — no need to build entire list in memory
- Ideal for processing large files, streaming data, or infinite sequences
- A generator that would produce 1 billion items uses the same memory as one producing 10
Sending Values Into Generators
gen.send(value)sends a value that becomes the result of theyieldexpressiongen.throw(Exception)raises an exception at the yield pointgen.close()raisesGeneratorExitat the yield point- Must call
next(gen)first to prime the generator before sending
Interview Insight
Common questions: "What's the difference between yield and return?" and "How do generators save memory?" Be able to write a generator from scratch and explain lazy evaluation.
Use Cases
Large file processing — reading files line by line without loading all into memory
Data pipelines — chaining generators for transform, filter, aggregate operations
Infinite sequences — Fibonacci, primes, IDs, timestamps
State machines — generators naturally maintain state between yields
Streaming APIs — paginating through large API result sets
Common Mistakes
Forgetting that generators are single-use — calling list() on it exhausts the generator
Not priming with next() before send() — raises TypeError
Using yield when you should return — generators are for sequences, not single values
Confusing generator functions with generator objects — calling the function returns the object
Not handling GeneratorExit in cleanup code — use try/finally for resource management