all Python 3astterm
ast.BoolOp
expressions
A boolean operation node for and or or: the operator in op and a list of operands in values. A chain of the same operator collapses into one node.
Syntax
ast.BoolOp(op, values)
a and b and c is a single BoolOp with three values, not nested pairs, unlike BinOp. Mixing operators nests by precedence: a and b or c gives an Or whose first value is a BoolOp with And. Negation with not is not here: it is a UnaryOp with Not. The bitwise & and | are not BoolOp either, they are BinOp.
Examples
import ast
node = ast.parse('a and b and c', mode='eval').body
print(type(node.op).__name__, len(node.values)) # → And 3
mixed = ast.parse('a and b or c', mode='eval').body
print(type(mixed.op).__name__, type(mixed.values[0].op).__name__) # → Or And
See also
Open in the full glossarydocs.python.orgReport an inaccuracy