Python syntax
Examples
Example 1
This Python code:
x + 1
...parses to this syntax tree:
Module
--.body[0]-->
Expr
--.value-->
BinOp
--.left-->
Name { id="x" }
--.op-->
Add
--.right-->
Constant { v=1, subtype=ConstantInt }
Links
Using Python's ast module
import ast
root = ast.parse(code)
Overview of nodes
Name | x |
Constant | str, bytes, int, float, True, False, None... |
List | [], [x], [x, y] |
Tuple | (), (x,), (x, y) |
Dict | {}, { k: v } |
ListComp, comprehension | [x for y in z if a if b] |
GeneratorExp, comprehension | (x for y in z if a if b) |
Delete | del x["y"] |
Attribute | foo.bar |
Subscript | foo["bar"], a[:-1] |
Slice | :, lower:upper:step, lower:upper, lower:, :upper |
Assign | x = 123, x = y = z = 123 |
AugAssign, ...BinOp operators... | x += 2, x *= 2, x <<= 2 |
UnaryOp | not x, ~x, +x, -x |
→ Not, Invert, UAdd, USub | |
BoolOp | x and y, x and y and z |
→ And, Or | |
BinOp | x + y, 2**128 |
→ Mult, Div, Add, Sub | |
→ Mod, Pow, MatMult, FloorDiv | |
→ BitAnd, BitOr, BitXor, LShift, RShift | |
Compare | x in arr, x < y <= z == a |
→ Eq, NotEq, Lt, LtE, Gt, GtE | |
→ Is, IsNot, In, NotIn | |
Return | return, return x |
Break | break |
Continue | continue |
Pass | pass |
Assert | assert x, assert x, msg |
Raise | raise NotImplementedError, raise Exception("Foo") from e |
IfExp | x if y else z |
If | if ... elif ... elif ... else ... |
For | for ..., for ... else ... |
While | while ..., while ... else ... |
FunctionDef, arguments, arg | def f(...): ... |
Lambda, arguments, arg | lambda x: x + 1 |
ClassDef | class Foo(Bar, Bar2): ... |
Call, keyword | f(x, foo=456), f(*args, **kwargs) |
Import, alias | import foo, numpy as np |
ImportFrom, alias | from foo import bar, bar2 as b, from foo import * |
Module | |
Expr | |
Comment (unofficial) | # ... |
Nodes
Name
| Python | AST |
|---|---|
foo | |
sauté | |
Constant
Examples...
| Python | Value type | AST |
|---|---|---|
True | bool | {"type": "Constant", "subtype": "ConstantTrue"} |
False | bool | {"type": "Constant", "subtype": "ConstantFalse"} |
None | NoneType | {"type": "Constant", "subtype": "ConstantNone"} |
123 | int | {"v": 123, "type": "Constant", "subtype": "ConstantInt"} |
123_000 | int | {"v": 123000, "type": "Constant", "subtype": "ConstantInt"} |
9007199254740993 | int | {"v": "9007199254740993", "type": "Constant", "subtype": "ConstantIntDigits"} |
18446744073709551616 | int | {"v": "18446744073709551616", "type": "Constant", "subtype": "ConstantIntDigits"} |
0x11 (16 + 1) | int | {"v": 17, "type": "Constant", "subtype": "ConstantIntHex"} |
0b11 (2 + 1) | int | {"v": 3, "type": "Constant", "subtype": "ConstantIntBinary"} |
0o11 (8 + 1) | int | {"v": 9, "type": "Constant", "subtype": "ConstantIntOctal"} |
0.5 | float | {"v": "0.5", "type": "Constant", "subtype": "ConstantFloatDecimal"} |
1e6 | float | {"v": "1000000.0", "type": "Constant", "subtype": "ConstantFloatDecimal"} |
1e-3 | float | {"v": "0.001", "type": "Constant", "subtype": "ConstantFloatDecimal"} |
1.5e6 | float | {"v": "1500000.0", "type": "Constant", "subtype": "ConstantFloatDecimal"} |
"123" | str | {"v": "123", "type": "Constant", "subtype": "ConstantStr"} |
"foo\nbar" | str | {"v": "foo\nbar", "type": "Constant", "subtype": "ConstantStr"} |
b"123" | bytes | {"hex": "313233", "type": "Constant", "subtype": "ConstantBytes"} |
b"PK\x03\x04" | bytes | {"hex": "504b0304", "type": "Constant", "subtype": "ConstantBytes"} |
List
A literal that creates an instance of <class 'list'> with zero or more elements.
| Python | AST |
|---|---|
[] | |
["foo"] | |
["foo", 123, x] | |
Tuple
A literal that creates an instance of <class 'tuple'> with zero or more elements.
| Python | AST |
|---|---|
() | |
(x,) | |
(x, y) | |
("foo", 123, x) | |
x, y = arr | |
return x, y | |
Dict
A literal that creates an instance of <class 'dict'> with zero or more (key, value) pairs.
| Python | AST |
|---|---|
{} | |
{ "foo": x, 123: y } | |
{ (1 + 2): "hello" } | |
{ x : y } | |
comprehension
See ListComp or GeneratorExp
ListComp
| Python | AST |
|---|---|
[x for y in z] | |
[x for y in z if a] | |
[x for y in z if a if b] | |
GeneratorExp
| Python | AST |
|---|---|
(x for y in z) | |
(x for y in z if a) | |
(x for y in z if a if b) | |
Delete
| Python | AST |
|---|---|
del row["foo"] | |
del foo.bar["item_" + str(i)] | |
Attribute
| Python | AST |
|---|---|
foo.bar | |
(v1 + v2).heading | |
Subscript
| Python | AST |
|---|---|
row["foo"] | |
a[0] | |
a[-1] | |
a[0:2] | |
a[0:2, 5:6] | |
Slice
| Python | AST |
|---|---|
a[lower:upper:step] | |
a[lower:upper] | |
a[lower:] | |
a[:upper] | |
a[:] | |
a[0:2] | |
a[0:-1] | |
list(range(2000))[0:1000:10] | |
Assign
| Python | AST |
|---|---|
x = 123 | |
x = y = f() | |
row["foo"] = 123 | |
x, y = arr | |
AugAssign
| Python | AST |
|---|---|
x += 2 | |
x /= 2 | |
foo.something -= 2 | |
row["foo"] -= 2 | |
See BinOp for other supported operators.
UnaryOp
Not
| Python | AST |
|---|---|
not x | |
Invert
| Python | AST |
|---|---|
~x | |
UAdd
| Python | AST |
|---|---|
+x | |
USub
| Python | AST |
|---|---|
-x | |
BoolOp
And
| Python | AST |
|---|---|
x and y | |
x and y and z | |
Or
| Python | AST |
|---|---|
x or y | |
x or y or z | |
BinOp
Mult
| Python | AST |
|---|---|
x * y | |
Div
| Python | AST |
|---|---|
x / y | |
Add
| Python | AST |
|---|---|
x + y | |
Sub
| Python | AST |
|---|---|
x - y | |
Mod
| Python | AST |
|---|---|
x % y | |
Pow
| Python | AST |
|---|---|
x**y | |
MatMult
| Python | AST |
|---|---|
x @ y | |
FloorDiv
| Python | AST |
|---|---|
x // y | |
BitAnd
| Python | AST |
|---|---|
0b0110 & 0b0011 | |
BitOr
| Python | AST |
|---|---|
0b0110 | 0b0011 | |
BitXor
| Python | AST |
|---|---|
0b0110 ^ 0b0011 | |
LShift
| Python | AST |
|---|---|
0b0110 << 1 | |
RShift
| Python | AST |
|---|---|
0b0110 >> 1 | |
Compare
Eq
| Python | AST |
|---|---|
x == y | |
NotEq
| Python | AST |
|---|---|
x != y | |
Lt
| Python | AST |
|---|---|
x < y | |
x < y <= z | |
LtE
| Python | AST |
|---|---|
x <= y | |
x < y <= z | |
Gt
| Python | AST |
|---|---|
x > y | |
GtE
| Python | AST |
|---|---|
x >= y | |
Is
| Python | AST |
|---|---|
x is True | |
IsNot
| Python | AST |
|---|---|
x is not None | |
In
| Python | AST |
|---|---|
x in y | |
NotIn
| Python | AST |
|---|---|
x not in y | |
Return
| Python | AST |
|---|---|
return | |
return x | |
return x, y | |
Break
| Python | AST |
|---|---|
break | |
Continue
| Python | AST |
|---|---|
continue | |
Pass
| Python | AST |
|---|---|
pass | |
Assert
| Python | AST |
|---|---|
assert x | |
assert x, msg | |
Raise
| Python | AST |
|---|---|
raise NotImplementedError | |
raise Exception("Foo") | |
raise FooError(x, y) from upstream_err | |
IfExp
| Python | AST |
|---|---|
x if y else z | |
If
| Python | AST |
|---|---|
| |
| |
| |
| |
| |
For
| Python | AST |
|---|---|
| |
| |
| |
| |
The else block executes after the loop is complete, unless the loop exited due to a break or an exception.
While
| Python | AST |
|---|---|
| |
| |
The else block executes after the test has evaluated to false. If that never happens (e.g. due to a break, a return, or an exception), then the else block will never get run.
arguments
See FunctionDef or Lambda.
arg
See FunctionDef or Lambda.
FunctionDef
| Python | AST |
|---|---|
| |
| |
| |
Lambda
| Python | AST |
|---|---|
lambda: 123 | |
lambda x, y=None: x + y | |
ClassDef
| Python | AST |
|---|---|
| |
| |
keyword
See Call
Call
| Python | AST |
|---|---|
f(123) | |
f(123, y=456, *a, **b) | |
alias
See Import or ImportFrom
Import
| Python | AST |
|---|---|
import foo, numpy as np | |
ImportFrom
| Python | AST |
|---|---|
from foo import * | |
from foo import bar, bar2 as b | |
Module
| Python | AST |
|---|---|
| |
Expr
| Python | AST |
|---|---|
| |
Comment
This is not an official node type — i.e. CPython's ast.parse() does not create any nodes for comments.