Python Glossary Русский

all Python 3astterm

ast.Lt

operators

The marker for the comparison < in an ast tree: it sits in Compare.ops and has no fields.

Syntax

ast.Lt()

Python parses '1 < x < 10' as a single comparison: Compare(left=1, ops=[Lt, Lt], comparators=[x, 10]). An analyzer looking for 'x is less than 10' must walk ops and comparators in pairs, not just inspect left. Compare marker nodes by type (isinstance), not with ==: ast nodes have no __eq__, so ast.Lt() == ast.Lt() is False.

Examples

import ast
c = ast.parse('1 < x < 10', mode='eval').body
pairs = [(type(o).__name__, ast.unparse(v)) for o, v in zip(c.ops, c.comparators)]
print(pairs)  # → [('Lt', 'x'), ('Lt', '10')]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy