The Python Language Reference, Release 3.9.5 67 6.2. Atoms generator object (in the same fashion as normal generators). However, the iterable expression in the leftmost$_{for}$ clause is immediately evaluated, so that an error produced by it will be emitted at the point where the generator expression is defined, rather than at the point where the first value is retrieved. Subsequent$_{for}$ clauses and any filter condition in the leftmost$_{for}$ clause cannot be evaluated in the enclosing scope as they may depend on the values obtained from the leftmost iterable. For example: (x*y for x in range(10) for y in range(x, x+10)) . The parentheses can be omitted on calls with only one argument. See section Calls for details. To avoid interfering with the expected operation of the generator expression itself,$_{yield}$ and$_{yield from}$ex- pressions are prohibited in the implicitly defined generator. If a generator expression contains either$_{async for}$clauses or $_{await}$expressions it is called an asynchronous generator expression . An asynchronous generator expression returns a new asynchronous generator object, which is an asynchronous iterator (see Asynchronous Iterators ). New in version 3.6: Asynchronous generator expressions were introduced. Changed in version 3.7: Prior to Python 3.7, asynchronous generator expressions could only appear in$_{async def}$ coroutines. Starting with 3.7, any function can use asynchronous generator expressions. Changed in version 3.8:$_{yield}$ and$_{yield from}$prohibited in the implicitly nested scope. yield_atom ::= "(" yield_expression ")" yield_expression ::= "yield" [ expression_list | "from" expression ] 6.2.9 Yield expressions The yield expression is used when defining a generator function or an asynchronous generator function and thus can only be used in the body of a function definition. Using a yield expression in a function’s body causes that function to be a generator, and using it in an$_{async def}$function’s body causes that coroutine function to be an asynchronous generator. For example: def gen(): # defines a generator function yield 123 async def agen(): # defines an asynchronous generator function yield 123 Due to their side effects on the containing scope, $_{yield}$expressions are not permitted as part of the implicitly defined scopes used to implement comprehensions and generator expressions. Changed in version 3.8: Yield expressions prohibited in the implicitly nested scopes used to implement comprehen- sions and generator expressions. Generator functions are described below, while asynchronous generator functions are described separately in section Asynchronous generator functions . When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of $_{expression_list}$to the generator’s caller. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the yield expression were just another external call. The value of the yield expression after resuming depends on the method which resumed the execution. If$_{__next__()}$ is used (typically via either a$_{for}$ or the$_{next()}$ builtin) then the result is$_{None}$ . Otherwise, if$_{send()}$ is used, then the result will be the value passed in to that method. All of this makes generator functions quite similar to coroutines; they yield multiple times, they have more than one entry point and their execution can be suspended. The only difference is that a generator function cannot control where the execution should continue after it yields; the control is always transferred to the generator’s caller.