---
source: https://iinventedlight.com/library/notes/python-ast.md
title: "Python ast - parse, walk, and safely evaluate Python syntax"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, ast - Abstract Syntax Trees; python/cpython (GitHub), Lib/ast.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: ast"
tags: [python, ast, parse, literal_eval, stdlib]
client: Librarian
sources:
  - id: S1
    title: "ast - Abstract Syntax Trees"
    publisher: Python docs
    url: https://docs.python.org/3/library/ast.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/ast.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/ast.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python ast - parse, walk, and safely evaluate Python syntax
**Answer:** Parse the source into an AST node with `ast.parse()`. `ast.literal_eval()` evaluates an expression node or a string containing only a Python literal or container display.

## Examples

```python
import ast

tree = ast.parse("x = 1 + 2")
print(ast.dump(tree, indent=2))
print(ast.unparse(tree))

print(ast.literal_eval("{'retries': 3, 'names': ['a', 'b']}"))
# ast.literal_eval("os.system('rm -rf /')")  # ValueError: malformed node

class Names(ast.NodeVisitor):
    def visit_Name(self, node):
        print(node.id, type(node.ctx).__name__)
        self.generic_visit(node)

Names().visit(tree)
```

## Rules

- `literal_eval` is for a Python literal or container display, not a general expression.
- `NodeVisitor` walks the tree and calls a visitor function for every node found.
- `ast.unparse()` generates source from a tree.

## See also

- [python-inspect](https://iinventedlight.com/library/notes/python-inspect/)
- [python-doctest](https://iinventedlight.com/library/notes/python-doctest/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## Claims

- id: C1
  claim: parse builds an AST from source.
  source: S1
  evidence: "Parse the source into an AST node"

- id: C2
  claim: literal_eval evaluates only Python literals.
  source: S1
  evidence: "Evaluate an expression node or a string containing only a Python literal"

- id: C3
  claim: literal_eval evaluates an expression node or a literal string.
  source: S2
  evidence: "Evaluate an expression node or a string containing"

## Sources

- [S1] ast - Abstract Syntax Trees. Python docs. https://docs.python.org/3/library/ast.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/ast.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/ast.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, ast - Abstract Syntax Trees; python/cpython (GitHub), Lib/ast.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
