On this page
  1. ast.walk
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

ast.walk

Prev: ast.unparse | Up: ast
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.walk
Tags: #public #python3 #stdlib #ast


Import

import ast
from ast import walk

Signature & Overview

ast.walk(node)

Recursively yield all descendant nodes in the tree starting at node (including node itself), in no specified order. This is useful if you only want to modify nodes in place and don't care about the context.

Usage

import ast

tree = ast.parse("def f(x): return x + 1")
names = [n.id for n in ast.walk(tree) if isinstance(n, ast.Name)]
assert names == ["x"]

Gotchas

REPL Inspect

from ast import walk
help(walk)

Linked from

Related notes