Python Glossary Русский

all Python 3astterm

ast.Compare

expressions

A comparison node: the left operand in left, plus lists of operators ops and right operands comparators. A chain a < b < c is a single node.

Syntax

ast.Compare(left, ops, comparators)

Even a single comparison a == b stores its operator and operand in lists: ops[0] and comparators[0]. A chain 0 < x <= 10 gives two operators and two operands, not a BoolOp with and — even though it runs like one. The in, not in, is and is not operators also land here. Assignment with = is not a comparison but a separate Assign statement.

Examples

import ast
node = ast.parse('0 < x <= 10', mode='eval').body
print(node.left.value, [type(op).__name__ for op in node.ops])  # → 0 ['Lt', 'LtE']
print(len(node.comparators))  # → 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy