Python Glossary Русский

Python 3.0+astterm

ast.Starred

variables

Node for a starred expression *value: unpacking in a call f(*args), in a literal [*a, *b], and a starred target a, *rest = …

Syntax

ast.Starred(value, ctx=ast.Load())

Starred covers only the single star: **kwargs in a call is a keyword with arg=None, and {**d} is a None key in the keys list of Dict. A function's *args parameter is not Starred either; it lives in the vararg field of arguments. Starred carries its own ctx: Store in an assignment target, Load in a call.

Examples

import ast
call = ast.parse('f(*args)', mode='eval').body
print(type(call.args[0]).__name__, call.args[0].value.id)  # → Starred args
import ast
target = ast.parse('a, *rest = data').body[0].targets[0]
star = target.elts[1]
print(type(star).__name__, star.value.id, type(star.ctx).__name__)  # → Starred rest Store

See also

Open in the full glossarydocs.python.orgReport an inaccuracy