all Python 3astterm
ast.NotEq
operators
The != comparison operator in the ops list of a Compare node.
Syntax
ast.NotEq()
Comparisons in ast are not binary: a != b != c is one Compare with two NotEq in ops and two comparators, not nested nodes, and it means a != b and b != c, so a and c may still be equal. The operator sits in a list, so testing for != is isinstance(n.ops[i], ast.NotEq), not an op field. The old <> is gone in Python 3: ast.parse raises SyntaxError.
Examples
import ast
n = ast.parse('a != b != c', mode='eval').body
print(type(n).__name__, [type(o).__name__ for o in n.ops], len(n.comparators)) # → Compare ['NotEq', 'NotEq'] 2
print(1 != 2 != 1) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy