all Python 3astterm
ast.Attribute
expressions
The node for attribute access obj.name: the owner expression in value, the attribute name as a string in attr and the context ctx (Load, Store or Del).
Syntax
ast.Attribute(value, attr, ctx)
The attr field is a plain string, not a Name node, so a visitor with visit_Name never sees attribute names. A chain a.b.c nests from the right: the outer node is the attribute c, and its value holds the Attribute for a.b. In a.b.c = 1 only the outer node gets Store; everything inside is read with Load.
Examples
import ast
node = ast.parse('a.b.c = 1').body[0].targets[0]
print(node.attr, type(node.ctx).__name__) # → c Store
print(node.value.attr, type(node.value.ctx).__name__) # → b Load
print(node.value.value.id) # → a
See also
Open in the full glossarydocs.python.orgReport an inaccuracy