I'm working with Abstract Syntax Trees in Python 3. The ast
library gives many ways to get children of the node (you can use iter_child_nodes()
or walk()
) but no ways to get parent of one. Also, every node has links to its children, but it hasn't links to its parent.
How I can get the parent of AST node if I don't want to write some plugin to ast
library?
What is the most correct way to do this?
You might create some hash table associating AST nodes to AST nodes and scan (recursively) your topmost AST tree to register in that hash table the parent of each node.