all Python 3astterm
ast.Assign
statements
The node of an assignment statement =: a list of targets, the value and type_comment. A chain a = b = 1 is one node with two targets.
Syntax
ast.Assign(targets, value, type_comment=None)
targets is a list for chains: a = b = 1 gives two targets, while unpacking a, b = 1, 2 gives one Tuple target. With an annotation (x: int = 1) it is AnnAssign, with += it is AugAssign, and x := 1 inside an expression is NamedExpr; finding every assignment means checking all four. Targets have the Store context.
Examples
import ast
node = ast.parse('a = b = 1').body[0]
print([t.id for t in node.targets], node.value.value) # → ['a', 'b'] 1
pair = ast.parse('a, b = 1, 2').body[0]
print(len(pair.targets), type(pair.targets[0]).__name__) # → 1 Tuple
See also
Open in the full glossarydocs.python.orgReport an inaccuracy