all Python 3astterm
ast.In
operators
The marker for the comparison in in an ast tree: it sits in Compare.ops and has no fields.
Syntax
ast.In()
'x in y' is a comparison (Compare), not a call; 'x not in y' is a single NotIn operator, while 'not x in y' is UnaryOp(Not) wrapping a Compare with In: same meaning, different trees. The in keyword of a for header is not an In operator at all — it is part of the For node. Compare marker nodes by type (isinstance), not with ==: ast nodes have no __eq__, so ast.In() == ast.In() is False.
Examples
import ast
print(type(ast.parse('x in y', mode='eval').body.ops[0]).__name__) # → In
print(type(ast.parse('x not in y', mode='eval').body.ops[0]).__name__) # → NotIn
print(type(ast.parse('not x in y', mode='eval').body).__name__) # → UnaryOp
See also
Open in the full glossarydocs.python.orgReport an inaccuracy