The Python Language Reference, Release 3.9.5 Chapter 6. Expressions 68 Yield expressions are allowed anywhere in a$_{try}$ construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator’s close() method will be called, allowing any pending$_{finally}$clauses to execute. When yield from <expr> is used, the supplied expression must be an iterable. The values produced by iterating that iterable are passed directly to the caller of the current generator’s methods. Any values passed in with$_{send()}$ and any exceptions passed in with$_{throw()}$ are passed to the underlying iterator if it has the appropriate methods. If this is not the case, then send() will raise AttributeError or TypeError , while throw() will just raise the passed in exception immediately. When the underlying iterator is complete, the value attribute of the raised StopIteration instance becomes the value of the yield expression. It can be either set explicitly when raising StopIteration , or automatically when the subiterator is a generator (by returning a value from the subgenerator). The parentheses may be omitted when the yield expression is the sole expression on the right hand side of an assign- ment statement. Changed in version 3.3: Added$_{yield from <expr>}$ to delegate control flow to a subiterator. See also: PEP 255 - Simple Generators The proposal for adding generators and the$_{yield}$ statement to Python. PEP 342 - Coroutines via Enhanced Generators The proposal to enhance the API and syntax of generators, mak- ing them usable as simple coroutines. PEP 380 - Syntax for Delegating to a Subgenerator The proposal to introduce the$_{yield_from}$syntax, mak- ing delegation to subgenerators easy. Generator-iterator methods PEP 525 - Asynchronous Generators The proposal that expanded on PEP 492 by adding generator capabilities to coroutine functions. This subsection describes the methods of a generator iterator. They can be used to control the execution of a generator function. Note that calling any of the generator methods below when the generator is already executing raises a$_{ValueError}$ exception. generator. __next__ () generator. send ( value$_{)}$ generator. throw ( type [, value [, traceback ] ] ) generator. close () Raises a$_{GeneratorExit}$ at the point where the generator function was paused. If the generator function then exits gracefully, is already closed, or raises $_{GeneratorExit}$(by not catching the exception), close Raises an exception of type $_{type}$at the point where the generator was paused, and returns the next value yielded by the generator function. If the generator exits without yielding another value, a$_{StopIteration}$ exception is raised. If the generator function does not catch the passed-in exception, or raises a different exception, then that exception propagates to the caller. Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The$_{send()}$ method returns the next value yielded by the generator, or raises $_{StopIteration}$if the generator exits without yielding another value. When$_{send()}$ is called to start the generator, it must be called with$_{None}$ as the argument, because there is no yield expression that could receive the value. This method is normally called implicitly, e.g. by a$_{for}$ loop, or by the built-in$_{next()}$ function. Starts the execution of a generator function or resumes it at the last executed yield expression. When a generator function is resumed with a$_{__next__()}$ method, the current yield expression always evaluates to$_{None}$ . The execution then continues to the next yield expression, where the generator is suspended again, and the value of the$_{expression_list}$is returned to$_{__next__()}$ ’s caller. If the generator exits without yielding another value, a$_{StopIteration}$ exception is raised.