all Python 3astterm
ast.And
operators
The class of the boolean operator and: an And() instance sits in the op field of a BoolOp. Its sibling is ast.Or.
Syntax
ast.And() # and
And appears only in a BoolOp — the bitwise & is BitAnd in a BinOp, and not is Not in a UnaryOp. All operands of a chain a and b and c sit in one BoolOp.values, so the number of conditions is len(node.values), not the nesting depth. The class name is capitalised: ast.and is a syntax error, because and is a keyword.
Examples
import ast
node = ast.parse('x > 0 and y > 0 and z', mode='eval').body
print(type(node.op).__name__, len(node.values)) # → And 3
print(type(ast.parse('a & b', mode='eval').body.op).__name__) # → BitAnd
See also
Open in the full glossarydocs.python.orgReport an inaccuracy