GNU Info

Info Node: (python2.1-ref.info)Augmented Assignment statements

(python2.1-ref.info)Augmented Assignment statements


Prev: Assignment statements Up: Assignment statements
Enter node , (file) or (file)node

Augmented Assignment statements
-------------------------------

Augmented assignment is the combination, in a single statement, of a
binary operation and an assignment statement:

     augmented_assignment_stmt: target augop expression_list
     augop:           "+=" | "-=" | "*=" | "/=" | "%=" | "**="
                    | ">>=" | "<<=" | "&=" | "^=" | "|="
     target:          identifier | "(" target_list ")" | "[" target_list "]"
                    | attributeref | subscription | slicing

(See section Note: Primaries for the syntax definitions for the last
three symbols.)

An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression list,
performs the binary operation specific to the type of assignment on the
two operands, and assigns the result to the original target.  The
target is only evaluated once.

An augmented assignment expression like `x += 1' can be rewritten as `x
= x + 1' to achieve a similar, but not exactly equal effect. In the
augmented version, `x' is only evaluated once. Also, when possible, the
actual operation is performed _in-place_, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead.

With the exception of assigning to tuples and multiple targets in a
single statement, the assignment done by augmented assignment
statements is handled the same way as normal assignments. Similarly,
with the exception of the possible _in-place_ behaviour, the binary
operation performed by augmented assignment is the same as the normal
binary operations.


automatically generated by info2www version 1.2.2.9