all Python 3astterm
ast.cmpop
operators
The abstract base class of comparison operators: Eq, NotEq, Lt, LtE, Gt, GtE, Is, IsNot, In, NotIn. Used in the ops list of a Compare node.
Syntax
isinstance(op, ast.cmpop)
The chain 0 < x <= 10 is one Compare with two operators in ops and two comparators, not an and of two comparisons. That is why Compare.ops is a list even for a single comparison. The compound 'is not' and 'not in' are separate classes IsNot and NotIn, not Not plus Is.
Examples
import ast
e = ast.parse('0 < x <= 10', mode='eval').body
print([type(o).__name__ for o in e.ops], len(e.comparators)) # → ['Lt', 'LtE'] 2
e = ast.parse('a not in b', mode='eval').body
print(type(e.ops[0]).__name__, isinstance(e.ops[0], ast.cmpop)) # → NotIn True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy