Mastery
Mastery/Python/B. Iteration & generators
T1 · high-leverage

Generators: lazy, one-shot, and why they save memory

A function with yield in its body is a generator function; calling it doesn't run the body — it returns a generator object (an iterator) that runs the body lazily, one yield at a time, on each next(). Compare memory behavior: a list comprehension builds the entire list up front; a generator expression produces values on demand and never holds more than one at a time.

python
def countdown(n):
    print(f"  (starting countdown from {n})")
    while n > 0:
        yield n
        n -= 1
    print("  (countdown done)")

gen = countdown(3)
print("generator created, body has NOT run yet:", gen)
print("first next():", next(gen))
print("second next():", next(gen))
print("third next():", next(gen))
try:
    next(gen)
except StopIteration:
    print("StopIteration once exhausted")
python
import sys

list_comp = [x * x for x in range(100_000)]
gen_exp = (x * x for x in range(100_000))

print("list comprehension size:", sys.getsizeof(list_comp), "bytes")
print("generator expression size:", sys.getsizeof(gen_exp), "bytes  <- constant, regardless of range size")
print("sum via generator (never materializes the full list):", sum(x * x for x in range(100_000)))

Interview angle

A staple "what will this print, and in what order?" question, because generator laziness is one of the cleanest ways to test whether someone actually traces execution rather than pattern-matching on syntax. Generator-writing exercises (e.g. "write a generator for the Fibonacci sequence") are popular precisely because they test syntax knowledge and an understanding of the memory/laziness tradeoff at the same time.

In the industry

Generators are everywhere in production Python: streaming large files or paginated API responses line by line, ETL pipelines, and ORM query iteration (Django, SQLAlchemy) all lean on them specifically to avoid materializing an entire dataset in memory at once. In code review, an eager list(...) wrapped around what could be a generator expression is a common efficiency comment, especially in code that processes large or unbounded input.