On this page
ast.iter_child_nodes
Prev: ast.increment_lineno | Up: ast | Next: ast.iter_fields
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.iter_child_nodes
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import iter_child_nodes
Signature & Overview
ast.iter_child_nodes(node)
Yield all direct child nodes of node, that is, all fields that are nodes and all items of fields that are lists of nodes.
Usage
import ast
tree = ast.parse("x = 1 + 2")
children = list(ast.iter_child_nodes(tree.body[0]))
assert len(children) == 2 # Name('x') and BinOp(1 + 2)
Gotchas
- Direct children only (1 level deep): Use
ast.walk()to traverse the whole subtree.
REPL Inspect
from ast import iter_child_nodes
help(iter_child_nodes)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/increment_lineno Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/iter_fields Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/Add Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/alias Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/And Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AnnAssign Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AST Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast/arg shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/arguments shared tags: #ast, #stdlib