A generator function is a specialized Python function that returns a lazy iterator (specifically, a generator object) instead of executing its body immediately and returning a single value. It is distinguished syntactically by the presence of one or moreDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
yield expressions. Rather than destroying its local state upon returning data, a generator function suspends its execution, preserves its local variables and instruction pointer, and yields control back to the caller.
Core Mechanics
1. Instantiation vs. Execution Invoking a generator function does not execute its code block. Instead, it instantiates and returns a generator object, which implements the Python iterator protocol (__iter__() and __next__()).
2. State Suspension (yield)
Execution of the function body begins only when next() (or the object’s __next__() method) is called. The interpreter executes the code until it encounters a yield expression. At this point, the function evaluates the expression, returns the result to the caller, and freezes its internal state.
3. Resumption
Upon subsequent calls to next(), the generator resumes execution immediately following the last executed yield statement, retaining full access to its previously established local scope and bindings.
4. Termination (StopIteration)
When the generator function reaches the end of its code block or encounters a return statement, it automatically raises a StopIteration exception. This signals to the caller (or a consuming for loop) that the iterator is exhausted. If a return statement includes a value (e.g., return "Done"), that value is attached to the StopIteration.value attribute.
Advanced Generator Interface
Because generator functions maintain state, Python extends their capabilities beyond simple iteration, allowing them to act as lightweight coroutines. The generator object exposes three methods to manipulate its suspended state:send(value): Resumes execution and injectsvalueinto the generator. Theyieldexpression where the generator is currently paused evaluates to this injected value. (Note: The first call to a generator must benext()orsend(None)to advance it to the firstyield).throw(type, [value, [traceback]]): Resumes execution by raising the specified exception at the exact point of the suspendedyield. The generator function can catch this exception using atry/exceptblock.close(): Raises aGeneratorExitexception at the suspendedyield. If the generator catches this exception, it must either raiseGeneratorExit, raiseStopIteration, or cleanly exit. ARuntimeErroris raised only if the generator attempts toyieldanother value after catchingGeneratorExit. If the generator raises any other exception during closure, that exception is propagated to the caller.
Delegation with yield from
A generator function can delegate part of its operations to another generator (or any iterable) using the yield from syntax. This establishes a transparent, bidirectional communication channel between the caller and the sub-generator, automatically handling next(), send(), throw(), and close() routing, as well as catching the sub-generator’s StopIteration value.
Master Python with Deep Grasping Methodology!Learn More





