A normal Python function runs to completion and hands back one value with return, and its local state is then discarded. A generator function contains yield, so calling it returns a generator iterator that produces values one at a time, pausing after each yield and resuming where it left off. Generators are the standard way to produce large or infinite sequences without holding them all in memory.
The sections below compare the two side by side, show how generators work internally, and cover generator expressions, yield from and send().
A normal function executes its body, reaches return (or the end) and finishes. Every call starts fresh.
A function that contains yield is a generator function. Calling it does not run the body. It creates a generator object, and code runs only when you ask for the next value.
Between calls to next(), the generator keeps its local variables and its position. When the body ends, the next next() raises StopIteration, which a for loop handles silently.
| Aspect | Normal function | Generator function |
|---|---|---|
| Keyword | return | yield (and optionally return) |
| Call result | The returned value | A generator iterator |
| Execution | Runs fully on each call | Runs lazily, one step per next() |
| State between values | Lost after return | Preserved locals and position |
| Memory | Whole result built at once | One item at a time |
| Reuse | Call again for a new run | Single pass; create a new generator to restart |
| Infinite output | Impossible | Natural |
Reading a big file or producing a long sequence into a list loads everything at once. A generator yields each item as needed.
first_errors never loads the whole file, and it stops reading as soon as it has enough matches. The same function written with lists would read and store every line first.
Because values are produced on demand, a generator can be infinite and still be safe to use, as long as the consumer stops.
Generators also chain into pipelines where each stage processes one item at a time:
A generator expression is the compact form, written like a list comprehension with parentheses. It builds nothing up front.
Use yield from to delegate to another iterable or generator.
Generators can also receive data. send(value) resumes the generator and makes the yield expression evaluate to that value.
For asynchronous work, async def functions with yield become asynchronous generators consumed with async for.
list(gen) when you need the full result after all, but remember a generator is exhausted after one pass.Yes. A bare return ends the generator. A returned value is carried inside the StopIteration exception and is rarely used directly, except with yield from.
Not necessarily. They save memory and allow early exit, but iterating a prebuilt list can be faster. Choose generators for memory and streaming, not raw speed.
You cannot. Call the generator function again to create a fresh generator.
Every generator is an iterator. Generators are a convenient way to write one without defining a class with __iter__ and __next__.
Normal functions compute and return a result once, while generator functions yield values lazily and keep their state between values. Use generators for large, streamed or infinite data, combine them into pipelines, and remember they can only be consumed once.