Python Glossary Русский

Python 3.5+astterm

ast.AsyncFunctionDef

async

The node of a coroutine definition async def: the same fields as FunctionDef — name, arguments, body, decorators, return annotation.

Syntax

ast.AsyncFunctionDef(name, args, body, decorator_list, returns=None, type_comment=None, type_params)

It is a separate class, not a subclass of FunctionDef: isinstance(node, ast.FunctionDef) is False for async def, and a NodeVisitor with only visit_FunctionDef skips every coroutine. The usual fix is visit_AsyncFunctionDef = visit_FunctionDef. Whether the body contains await does not affect the node type — only the async keyword does.

Examples

import ast
node = ast.parse('async def fetch(url):\n    return url').body[0]
print(type(node).__name__, node.name, node.args.args[0].arg)  # → AsyncFunctionDef fetch url
print(isinstance(node, ast.FunctionDef))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy