Python Glossary Русский

all Python 3astterm

ast.AugAssign

statements

The node of an augmented assignment x += 1, x *= 2 and so on: the target, the operator and the value. The operator is the same class as in BinOp.

Syntax

ast.AugAssign(target, op, value)

There are no IAdd-style classes: op holds the ordinary Add, Mult and so on, and += is told apart from + only by the type of the node itself. There is a single target and it cannot be a tuple: a, b += 1 is a syntax error; its context is Store even though the value is read too. The old AugLoad and AugStore contexts never appear here.

Examples

import ast
node = ast.parse('total *= 2').body[0]
print(node.target.id, type(node.op).__name__, node.value.value)  # → total Mult 2
print(type(node.target.ctx).__name__)  # → Store
import ast
try:
    ast.parse('a, b += 1')
except SyntaxError:
    print('tuple target refused')  # → tuple target refused

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy