Python Glossary Русский

all Python 3astterm

ast.Yield

functions and classes

Node for a yield expression: value is the yielded value or None. Used as a statement, it sits inside an Expr.

Syntax

ast.Yield(value=None)

yield is an expression, not a statement: the line yield x is Expr(Yield), and x = yield is an Assign with a Yield on the right whose value is None. yield from is a separate YieldFrom node. ast does not mark generator functions: FunctionDef has no such flag, so you can tell only by searching the body for Yield without descending into nested functions or lambdas.

Examples

import ast
f = ast.parse('def g():\n    yield 1\n    x = yield').body[0]
print(type(f.body[0]).__name__, type(f.body[0].value).__name__)  # → Expr Yield
print(f.body[1].value.value)  # → None

See also

Open in the full glossarydocs.python.orgReport an inaccuracy