Python Glossary Русский

all Python 3astterm

ast.Eq

operators

The class of the comparison operator ==: an Eq() instance lies in the ops list of a Compare node. Its siblings are NotEq, Lt, Is, In and the others.

Syntax

ast.Eq()  # ==

Comparison operators are stored in a list, so for a == b read node.ops[0], not node.op — a Compare has no such field. Linters find x == None exactly as a Compare with Eq and Constant(None); the correct form is uses the Is class. Assignment with = has nothing to do with Eq: it is an Assign statement.

Examples

import ast
node = ast.parse('x == None', mode='eval').body
print(type(node.ops[0]).__name__, node.comparators[0].value)  # → Eq None
print(hasattr(node, 'op'))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy