Python Glossary Русский

all Python 3astterm

ast.keyword

expressions

The node for a keyword argument in a call f(key=value) or in a class definition class C(metaclass=M). Fields: arg, the name as a string, and value, an expression.

Syntax

ast.keyword(arg, value)

Unpacking f(**opts) is also a keyword, but with arg=None: code that treats kw.arg as a string crashes on such calls. Keyword arguments live in Call.keywords, not in Call.args; positional unpacking *args is a Starred inside args. Unlike arg from a function definition, a keyword has no annotation.

Examples

import ast
call = ast.parse('print(a, sep="-", **opts)', mode='eval').body
for kw in call.keywords:
    print(kw.arg, ast.unparse(kw.value))  # → sep '-' / None opts
print(len(call.args))  # → 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy