Python Glossary Русский

all Python 3astterm

ast.Add

operators

The class of the addition operator +: an Add() instance sits in the op field of a BinOp (a + b) and of an AugAssign (a += b).

Syntax

ast.Add()  # +

There is no separate class for +=: AugAssign stores the same Add, so searching additions through BinOp alone misses augmented assignments. String and list concatenation is Add as well: the parser knows nothing about operand types. Unary plus +x is not Add but UAdd inside a UnaryOp. The node has no fields or positions, and the parser hands out one shared instance, so test with isinstance(op, ast.Add).

Examples

import ast
print(type(ast.parse('a + b', mode='eval').body.op).__name__)  # → Add
print(type(ast.parse('a += b').body[0].op).__name__)  # → Add
print(type(ast.parse('+a', mode='eval').body.op).__name__)  # → UAdd

See also

Open in the full glossarydocs.python.orgReport an inaccuracy