Python Glossary Русский

all Python 3astterm

ast.Call

expressions

The node for a call f(...): the callable expression in func, positional arguments in args and named ones in keywords.

Syntax

ast.Call(func, args, keywords)

Unpackings in a call are not where you might expect: *items goes into args as a Starred node, and **options goes into keywords as a keyword with arg=None. The func field is any expression: for obj.method() it is an Attribute, for f()() another Call, so the function name is not simply node.func.id. A bare print(1) line is an Expr with a Call inside.

Examples

import ast
node = ast.parse('f(1, *rest, key=2, **opts)', mode='eval').body
print(node.func.id, [type(a).__name__ for a in node.args])  # → f ['Constant', 'Starred']
print([k.arg for k in node.keywords])  # → ['key', None]
import ast
node = ast.parse('obj.save()', mode='eval').body
print(type(node.func).__name__, node.func.attr)  # → Attribute save

See also

Open in the full glossarydocs.python.orgReport an inaccuracy