
Specific Statements
|
39
Augmented assignment
A set of additional assignment statement formats, listed in
Table 13, are available. Known as augmented assignments,
these formats imply a binary expression plus an assignment.
For instance, the following two formats are roughly equivalent:
X = X + Y
X += Y
However, the reference to target X in the second format needs
to be evaluated only once, and in-place operations can be
applied for mutables as an optimization (e.g.,
list1 += list2
automatically calls list1.extend(list2), instead of the slower
concatenation operation implied by
+). Classes can overload
in-place assignments with method names that begin with an
i
(e.g., __iadd__ for +=, __add__ for +). The format X //= Y
(floor division) is new as of Version 2.2.
Expressions
expression
function([value, name=value, ...])
object.method([value, name=value, ...])
Any expression can appear as a statement (but statements
cannot appear as expressions). Expressions are commonly
used for calling functions and for interactive-mode printing.
In function and method calls, actual arguments are sepa-
rated by commas and are normally matched to arguments in
function
def headers by position. Calls can optionally list
specific argument names in functions to receive passed val-
ues by using the
name=value keyword syntax.
Table 13. Augmented assignment statements
X += Y X &= Y X -= Y X |= Y
X *= Y X ^= Y X /= Y X >>= Y
X %= Y X <<= Y