all Python 3astterm
ast.Store
variables
The "store" context of Name, Attribute, Subscript, Starred, List and Tuple: the node is an assignment target, a for target or a with … as target.
Syntax
ast.Store()
Store appears not only in x = 1: the target is also marked Store in x += 1, for x in …, with f() as x and (x := 1), while import uses alias with a plain string instead. The parser reuses a single Store object for every node, so node.ctx is other_ctx is true; never set attributes on a ctx, the change leaks across the tree. Test isinstance(node.ctx, ast.Store) rather than comparing with ast.Store().
Examples
import ast
tree = ast.parse('for i in items:\n total += i')
stored = [n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Store)]
print(sorted(stored)) # → ['i', 'total']
import ast
a = ast.parse('x = 1').body[0].targets[0].ctx
b = ast.parse('y = 2').body[0].targets[0].ctx
print(a is b, a == ast.Store()) # → True False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy