⚙️Abstract Syntax Tree (AST)
Also known as AST, Syntax Tree
A tree that shows how the parts of a piece of code fit together.
Think of it like
Think of it like a family tree for a line of code. Each part hangs under the part it belongs to.
Example
Python turns x = 1 + 2 into a tree. At the top is the assignment. Under it are the name x and an addition, with 1 and 2 below that.
Why it matters
Tools that fix, check or change code safely all read the tree. That is how a rename touches only the right names.
Definition
Example code
console
$ python3 -c 'import ast; print(ast.dump(ast.parse("x = 1 + 2").body[0]))'
Assign(targets=[Name(id='x', ctx=Store())], value=BinOp(left=Constant(value=1), op=Add(), right=Constant(value=2)))Common mistake
Key takeaways
- 1Built by the parser. From the raw text.
- 2Meaning only. No spaces or brackets.
- 3Tools work on it. Compilers, linters, formatters.