Python Glossary Русский

all Python 3astterm

ast.operator

operators

The abstract base class of binary arithmetic and bitwise operators: Add, Sub, Mult, Div, FloorDiv, Mod, Pow, MatMult, shifts and bitwise ones.

Syntax

isinstance(node.op, ast.operator)

The same operator class appears both in BinOp (a + b) and in AugAssign (a += b): op cannot tell them apart, look at the node type itself. Comparisons (<, ==) and logic (and) are not here; they have their own cmpop and boolop. Parsed operator instances are shared: every Add() in a tree is the same object.

Examples

import ast
tree = ast.parse('y = a + b\ny += 1')
ops = [n.op for n in ast.walk(tree) if isinstance(n, (ast.BinOp, ast.AugAssign))]
print([type(o).__name__ for o in ops], ops[0] is ops[1])  # → ['Add', 'Add'] True
print(len(ast.operator.__subclasses__()))  # → 13

See also

Open in the full glossarydocs.python.orgReport an inaccuracy