Python Glossary Русский

Python 3.0+astterm

ast.arg

functions and classes

The node for one parameter in a function definition: the arg field is the name as a string, annotation is the annotation or None. A building block of arguments.

Syntax

ast.arg(arg, annotation=None)

The parameter name lives in a field of the same name: p.arg, not p.id or p.name. The default value is not stored in arg; it is in arguments.defaults or kw_defaults. Do not confuse it with keyword: keyword is an argument in a call f(x=1), arg is a parameter in a definition def f(x=1). arg carries lineno positions.

Examples

import ast
f = ast.parse('def f(n: int, *items: str): pass').body[0]
p = f.args.args[0]
print(type(p).__name__, p.arg, ast.unparse(p.annotation))  # → arg n int
print(f.args.vararg.arg, ast.unparse(f.args.vararg.annotation))  # → items str

See also

Open in the full glossarydocs.python.orgReport an inaccuracy