Python Glossary Русский

Python 3.3+astterm

ast.YieldFrom

functions and classes

The node for the expression yield from <iterable>: delegation to a subgenerator. The value field is the expression values are taken from. Added in 3.3.

Syntax

ast.YieldFrom(value)

It is an expression, not a statement: the line yield from g() parses as an Expr whose value is YieldFrom, and r = yield from g() as an Assign. A function containing YieldFrom is a generator, and the only sign of that is the node in its body. ast.parse does not check that yield from is inside a function; only compile fails.

Examples

import ast
g = ast.parse('def g():\n    r = yield from range(3)').body[0]
node = g.body[0].value
print(type(g.body[0]).__name__, type(node).__name__, ast.unparse(node.value))
# → Assign YieldFrom range(3)
import ast
tree = ast.parse('yield from x')
print(type(tree.body[0].value).__name__)  # → YieldFrom
try:
    compile(tree, '<ast>', 'exec')
except SyntaxError as e:
    print(type(e).__name__)  # → SyntaxError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy