Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects:
assignment_stmt ::= ([target_list] “=”)+ [ expression_list]
target_list ::= [ target] (“,” [target])* [“,”]
target ::= [ identifier]
| "(" [target\_list] ")"
| "[" [target\_list] "]"
| [ attributeref]
| [ subscription]
(See section [5.3] for the syntax definitions for the last three symbols.)
An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section [3.2]).
Assignment of an object to a target list is recursively defined as follows.
If the target list is a single target: The object is assigned to that target.
If the target list is a comma-separated list of targets: The object must be a sequence with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. (This rule is relaxed as of Python 1.5; in earlier versions, the object had to be a tuple. Since strings are sequences, an assignment like “a, b = “xy”” is now legal as long as the string has the right length.)
Assignment of an object to a single target is recursively defined as follows.
If the target is an identifier (name):
- If the name does not occur in a global statement in the current code block: the name is bound to the object in the current local namespace.
- Otherwise: the name is bound to the object in the current global namespace.
The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called.
If the target is a target list enclosed in parentheses or in square brackets: The object must be a sequence with the same number of items as there are targets in the target list, and its items are assigned, from left to right, to the corresponding targets.
If the target is an attribute reference, see attribute-references.
If the target is a subscription, see subscriptions.
If the target is a slicing, see slicings.
Note: Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are `safe’ (for example “a, b = b, a” swaps two variables), overlaps within the collection of assigned-to variables are not safe! For instance, the following program prints “[0, 2]”:
x = [0, 1] i = 0 i, x[i] = 1, 2 print x
Augmented assignment statements
Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:
where operation= is one of
+= -= *= /= %= **= >>= <<= &= ^= |=
An augmented assignment evaluates the target (which must be a single target, not a target list) 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 behavior, the binary operation performed by augmented assignment is the same as the normal binary operations.
For targets which are attribute references, the initial value is retrieved with a getattr and the result is assigned with a setattr. Notice that the two methods do not necessarily refer to the same variable. When getattr refers to a class variable, setattr still writes to an instance variable. For example:
class A: x = 3 # class variable a = A() a.x += 1 # writes a.x as 4 leaving A.x as 3