all Python 3astterm
ast.Not
operators
The logical negation operator not, found in the op field of a UnaryOp.
Syntax
ast.Not()
not is a unary operation, so it lives in UnaryOp, whereas and and or live in BoolOp. The compound not in is a separate NotIn operator inside Compare, not Not; yet not a in b parses as UnaryOp(Not, Compare(In)). The result is the same, but a linter rule "prefer not in" looks for exactly the second shape.
Examples
import ast
n = ast.parse('not a in b', mode='eval').body
print(type(n).__name__, type(n.op).__name__, type(n.operand.ops[0]).__name__) # → UnaryOp Not In
m = ast.parse('a not in b', mode='eval').body
print(type(m).__name__, type(m.ops[0]).__name__) # → Compare NotIn
See also
Open in the full glossarydocs.python.orgReport an inaccuracy