Online Book Reader

Home Category

Learning Python - Mark Lutz [170]

By Root 1635 0
the last example of the prior section becomes even simpler, since we don’t have to manually slice to get the first and rest of the items:

>>> L = [1, 2, 3, 4]

>>> while L:

... front, *L = L # Get first, rest without slicing

... print(front, L)

...

1 [2, 3, 4]

2 [3, 4]

3 [4]

4 []

Boundary cases

Although extended sequence unpacking is flexible, some boundary cases are worth noting. First, the starred name may match just a single item, but is always assigned a list:

>>> seq

[1, 2, 3, 4]

>>> a, b, c, *d = seq

>>> print(a, b, c, d)

1 2 3 [4]

Second, if there is nothing left to match the starred name, it is assigned an empty list, regardless of where it appears. In the following, a, b, c, and d have matched every item in the sequence, but Python assigns e an empty list instead of treating this as an error case:

>>> a, b, c, d, *e = seq

>>> print(a, b, c, d, e)

1 2 3 4 []

>>> a, b, *e, c, d = seq

>>> print(a, b, c, d, e)

1 2 3 4 []

Finally, errors can still be triggered if there is more than one starred name, if there are too few values and no star (as before), and if the starred name is not itself coded inside a sequence:

>>> a, *b, c, *d = seq

SyntaxError: two starred expressions in assignment

>>> a, b = seq

ValueError: too many values to unpack

>>> *a = seq

SyntaxError: starred assignment target must be in a list or tuple

>>> *a, = seq

>>> a

[1, 2, 3, 4]

A useful convenience

Keep in mind that extended sequence unpacking assignment is just a convenience. We can usually achieve the same effects with explicit indexing and slicing (and in fact must in Python 2.X), but extended unpacking is simpler to code. The common “first, rest” splitting coding pattern, for example, can be coded either way, but slicing involves extra work:

>>> seq

[1, 2, 3, 4]

>>> a, *b = seq # First, rest

>>> a, b

(1, [2, 3, 4])

>>> a, b = seq[0], seq[1:] # First, rest: traditional

>>> a, b

(1, [2, 3, 4])

The also common “rest, last” splitting pattern can similarly be coded either way, but the new extended unpacking syntax requires noticeably fewer keystrokes:

>>> *a, b = seq # Rest, last

>>> a, b

([1, 2, 3], 4)

>>> a, b = seq[:-1], seq[-1] # Rest, last: traditional

>>> a, b

([1, 2, 3], 4)

Because it is not only simpler but, arguably, more natural, extended sequence unpacking syntax will likely become widespread in Python code over time.

Application to for loops

Because the loop variable in the for loop statement can be any assignment target, extended sequence assignment works here too. We met the for loop iteration tool briefly in Part II and will study it formally in Chapter 13. In Python 3.0, extended assignments may show up after the word for, where a simple variable name is more commonly used:

for (a, *b, c) in [(1, 2, 3, 4), (5, 6, 7, 8)]:

...

When used in this context, on each iteration Python simply assigns the next tuple of values to the tuple of names. On the first loop, for example, it’s as if we’d run the following assignment statement:

a, *b, c = (1, 2, 3, 4) # b gets [2, 3]

The names a, b, and c can be used within the loop’s code to reference the extracted components. In fact, this is really not a special case at all, but just an instance of general assignment at work. As we saw earlier in this chapter, we can do the same thing with simple tuple assignment in both Python 2.X and 3.X:

for (a, b, c) in [(1, 2, 3), (4, 5, 6)]: # a, b, c = (1, 2, 3), ...

And we can always emulate 3.0’s extended assignment behavior in 2.6 by manually slicing:

for all in [(1, 2, 3, 4), (5, 6, 7, 8)]:

a, b, c = all[0], all[1:3], all[3]

Since we haven’t learned enough to get more detailed about the syntax of for loops, we’ll return to this topic in Chapter 13.

Multiple-Target Assignments

A multiple-target assignment simply assigns all the given names to the object all the way to the right. The following, for example, assigns the three variables a, b, and c to the string 'spam':

>>> a = b = c = 'spam'

>>> a, b, c

('spam', 'spam', 'spam')

Return Main Page Previous Page Next Page

®Online Book Reader