all Python 3astterm
ast.BitAnd
operators
The class of the bitwise operator &: a BitAnd() instance sits in the op of a BinOp or an AugAssign (a &= b). The same node also represents set intersection.
Syntax
ast.BitAnd() # &
The parser knows no types, so s1 & s2 for sets and 6 & 3 for numbers give the same BinOp with BitAnd. The logical and is unrelated — it is And in a BoolOp, and mixing them up in an analyser causes false matches. The node has no fields: test isinstance(node.op, ast.BitAnd).
Examples
import ast
for src in ['6 & 3', 's1 & s2', 'a and b']:
node = ast.parse(src, mode='eval').body
print(type(node).__name__, type(node.op).__name__)
# → BinOp BitAnd / BinOp BitAnd / BoolOp And
See also
Open in the full glossarydocs.python.orgReport an inaccuracy