logo
55

Generators

⏱️ 30 min

Generators: Produce on Demand, Save Memory

What might confuse you right now

"Lists work fine. Why do I need generators?"

When your data is huge, a list loads everything into memory at once. A generator computes and yields values one at a time -- much better for streaming.

One-line definition

A generator is a function containing yield that returns one value per iteration and preserves its execution state.

Real-life analogy

A list is like hauling an entire case of water home at once. A generator is like grabbing one bottle each time you're thirsty.

Minimal runnable example

def count_up_to(n):
    i = 1
    while i <= n:
        yield i
        i += 1

for x in count_up_to(3):
    print(x)

Quick quiz (5 min)

  1. Write count_step(start, end, step).
  2. Use next() to manually pull 3 values.
  3. Observe what happens after exhaustion.

Quiz answer guidelines & grading criteria

  • Answer direction: working code that covers core conditions and edge inputs from the prompt.
  • Criterion 1 (Correctness): Main flow produces correct results, key branches execute.
  • Criterion 2 (Readability): Clear variable names, no excessive nesting.
  • Criterion 3 (Robustness): Basic protection against null values, type errors, or unexpected input.

Transfer task (homework)

Implement read_large_file(path) that yields line by line and counts keyword hits.

Acceptance criteria

You can independently:

  • Explain the difference between yield and return
  • Write a consumable generator
  • Choose appropriate scenarios for generators

Common errors & debugging steps (beginner edition)

  • Can't understand the error: read the last line for the error type (e.g., TypeError, NameError), then trace back to the relevant code line.
  • Not sure about a variable's value: temporarily add print(variable, type(variable)) at key points to verify data matches expectations.
  • Code changes aren't taking effect: confirm the file is saved, you're running the right file, and your terminal environment (venv) is correct.

Common misconceptions

  • Misconception: Generators can be iterated over multiple times.
  • Reality: Once exhausted, you need to create a new one.