Online Book Reader

Home Category

Learning Python - Mark Lutz [171]

By Root 1803 0

This form is equivalent to (but easier to code than) these three assignments:

>>> c = 'spam'

>>> b = c

>>> a = b

Multiple-target assignment and shared references

Keep in mind that there is just one object here, shared by all three variables (they all wind up pointing to the same object in memory). This behavior is fine for immutable types—for example, when initializing a set of counters to zero (recall that variables must be assigned before they can be used in Python, so you must initialize counters to zero before you can start adding to them):

>>> a = b = 0

>>> b = b + 1

>>> a, b

(0, 1)

Here, changing b only changes b because numbers do not support in-place changes. As long as the object assigned is immutable, it’s irrelevant if more than one name references it.

As usual, though, we have to be more cautious when initializing variables to an empty mutable object such as a list or dictionary:

>>> a = b = []

>>> b.append(42)

>>> a, b

([42], [42])

This time, because a and b reference the same object, appending to it in-place through b will impact what we see through a as well. This is really just another example of the shared reference phenomenon we first met in Chapter 6. To avoid the issue, initialize mutable objects in separate statements instead, so that each creates a distinct empty object by running a distinct literal expression:

>>> a = []

>>> b = []

>>> b.append(42)

>>> a, b

([], [42])

Augmented Assignments

Beginning with Python 2.0, the set of additional assignment statement formats listed in Table 11-2 became available. Known as augmented assignments, and borrowed from the C language, these formats are mostly just shorthand. They imply the combination of a binary expression and an assignment. For instance, the following two formats are now roughly equivalent:

X = X + Y # Traditional form

X += Y # Newer augmented form

Table 11-2. Augmented assignment statements

X += Y

X &= Y

X -= Y

X |= Y

X *= Y

X ^= Y

X /= Y

X >>= Y

X %= Y

X <<= Y

X **= Y

X //= Y

Augmented assignment works on any type that supports the implied binary expression. For example, here are two ways to add 1 to a name:

>>> x = 1

>>> x = x + 1 # Traditional

>>> x

2

>>> x += 1 # Augmented

>>> x

3

When applied to a string, the augmented form performs concatenation instead. Thus, the second line here is equivalent to typing the longer S = S + "SPAM":

>>> S = "spam"

>>> S += "SPAM" # Implied concatenation

>>> S

'spamSPAM'

As shown in Table 11-2, there are analogous augmented assignment forms for every Python binary expression operator (i.e., each operator with values on the left and right side). For instance, X *= Y multiplies and assigns, X >>= Y shifts right and assigns, and so on. X //= Y (for floor division) was added in version 2.2.

Augmented assignments have three advantages:[25]

There’s less for you to type. Need I say more?

The left side only has to be evaluated once. In X += Y, X may be a complicated object expression. In the augmented form, it only has to be evaluated once. However, in the long form, X = X + Y, X appears twice and must be run twice. Because of this, augmented assignments usually run faster.

The optimal technique is automatically chosen. That is, for objects that support in-place changes, the augmented forms automatically perform in-place change operations instead of slower copies.

The last point here requires a bit more explanation. For augmented assignments, in-place operations may be applied for mutable objects as an optimization. Recall that lists can be extended in a variety of ways. To add a single item to the end of a list, we can concatenate or call append:

>>> L = [1, 2]

>>> L = L + [3] # Concatenate: slower

>>> L

[1, 2, 3]

>>> L.append(4) # Faster, but in-place

>>> L

[1, 2, 3, 4]

And to add a set of items to the end, we can either concatenate again or call the list extend method:[26]

>>> L = L + [5, 6] # Concatenate: slower

>>> L

[1, 2, 3, 4, 5, 6]

>>> L.extend([7, 8]) # Faster, but in-place

>>>

Return Main Page Previous Page Next Page

®Online Book Reader