all Python 3astterm
ast.Name
variables
Node for a variable name: the id field holds the name as a string, ctx is Load, Store or Del depending on whether the name is read, assigned or deleted.
Syntax
ast.Name(id, ctx=ast.Load())
Not every word in code is a Name: since Python 3.8 True, False and None parse as Constant, and in a.b only a is a Name — b is stored as a string in the attr field of Attribute. Searching for uses of x by Name without checking ctx mixes reads and assignments; filter on isinstance(node.ctx, ast.Load). Function parameter names are arg nodes, not Name.
Examples
import ast
n = ast.parse('x = y').body[0]
print(n.targets[0].id, type(n.targets[0].ctx).__name__) # → x Store
print(n.value.id, type(n.value.ctx).__name__) # → y Load
import ast
print(type(ast.parse('True', mode='eval').body).__name__) # → Constant
a = ast.parse('obj.size', mode='eval').body
print(type(a.value).__name__, a.value.id, a.attr) # → Name obj size
See also
Open in the full glossarydocs.python.orgReport an inaccuracy