all Python 3astterm
ast.IfExp
expressions
The node for a conditional expression a if cond else b: test is the condition, body the value when true, orelse the value when false.
Syntax
ast.IfExp(test, body, orelse)
Field order differs from source order: the value comes first in the code, but body follows test in the node. It is an expression, not a statement: IfExp has no blocks and no elif — the chain 'a if p else b if q else c' gives an IfExp nested in orelse. The if … : statement is a different node, If.
Examples
import ast
e = ast.parse('a if p else b if q else c', mode='eval').body
print(type(e).__name__, e.test.id, e.body.id) # → IfExp p a
print(type(e.orelse).__name__, e.orelse.test.id) # → IfExp q
See also
Open in the full glossarydocs.python.orgReport an inaccuracy