all Python 3astterm
ast.boolop
operators
The abstract base class of the boolean operators And and Or. An instance sits in the op field of a BoolOp node.
Syntax
isinstance(node.op, ast.boolop)
The chain a and b and c is a single BoolOp with three values, not nested pairs; the mixed a or b and c is BoolOp(Or) with a nested BoolOp(And), following precedence. not does not belong here: it is the unary Not from unaryop. Check the operator type with isinstance(node.op, ast.And), not with ==.
Examples
import ast
e = ast.parse('a and b and c', mode='eval').body
print(type(e.op).__name__, isinstance(e.op, ast.boolop), len(e.values)) # → And True 3
print([c.__name__ for c in ast.boolop.__subclasses__()]) # → ['And', 'Or']
import ast
e = ast.parse('a or b and c', mode='eval').body
print(type(e.op).__name__, type(e.values[1].op).__name__) # → Or And
See also
Open in the full glossarydocs.python.orgReport an inaccuracy