Python Glossary Русский

Python 3.5+astterm

ast.Await

async

The node of an await expression: the awaited object in the value field. Most often it sits inside an Expr or on the right of an assignment.

Syntax

ast.Await(value)

Await is an expression, not a statement: the line await g() in a body is Expr(Await(Call)), and the call is two levels down. ast.parse('await x') at module level succeeds, and only compile() reports 'await' outside function (unless the PyCF_ALLOW_TOP_LEVEL_AWAIT flag is set, which python -m asyncio relies on).

Examples

import ast
fn = ast.parse('async def f():\n    await g()').body[0]
stmt = fn.body[0]
print(type(stmt).__name__, type(stmt.value).__name__, stmt.value.value.func.id)  # → Expr Await g
import ast
tree = ast.parse('await x')
try:
    compile(tree, '<src>', 'exec')
except SyntaxError as e:
    print(e.msg)  # → 'await' outside function

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy