all Python 3astfunction
ast.dump()
helper functions
Returns a text representation of an AST with node and field names. The main tool for seeing how Python sees a piece of code.
Syntax
ast.dump(node, annotate_fields=True, include_attributes=False, *, indent=None, show_empty=False)
The output depends on the version: since 3.13 empty lists (type_ignores=[]) are omitted by default, since 3.15 so is ctx=Load(); show_empty=True brings them back. So never compare dump strings across Python versions. indent=4 prints the tree as an indented ladder (since 3.9), and lineno/col_offset appear only with include_attributes=True. Since 3.14 repr(node) shows fields too; before it showed only the object address.
Examples
import ast
tree = ast.parse('42', mode='eval')
print(ast.dump(tree)) # → Expression(body=Constant(value=42))
print(ast.dump(tree, annotate_fields=False)) # → Expression(Constant(42))
import ast
tree = ast.parse('42', mode='eval')
print(ast.dump(tree, include_attributes=True))
# → Expression(body=Constant(value=42, lineno=1, col_offset=0, end_lineno=1, end_col_offset=2))
See also
Open in the full glossarydocs.python.orgReport an inaccuracy