Python Part I · Module 2 · Variables and Data Types
Assigning Several Variables at Once
Python lets you assign several names on one line, which is occasionally very useful and occasionally a way to hide what a line is doing. This chapter covers the three forms you will meet, starting with the ordinary one — a row of names on the left, a row of values on the right, matched up in order. From that single rule the well-known one-line swap falls out for free, and it works for exactly the reason the previous chapter gave: Python works out the entire right-hand side before it assigns anything. The chapter finishes with the judgement half, because the compact form is genuinely better for two or three closely related values and genuinely worse for five unrelated ones.
Foundation7 min readPythonPart IFundamentalsAssignmentReadability
Part I · Module 2 · Chapter 2.5 · Assigning Several Variables at Once
1. Several Names, Several Values
You already know how to do this the long way:
test_name = "axi_smoke"
seed = 4210Python also allows both on one line:
test_name, seed = "axi_smoke", 4210
print(test_name)
print(seed)axi_smoke
4210The rule is positional: the names on the left are matched with the values on the right, left to right. First name gets first value, second name gets second value.
The counts must match. If they do not, Python says so:
a, b = 1, 2, 3ValueError: too many values to unpack (expected 2)That message is worth reading rather than skipping. "Expected 2" is the number of names you wrote; Python found three values to hand out and had nowhere to put the third.
2. The Swap
Here is the form that earns the syntax its place. Suppose two names refer to two values and you want them exchanged.
Doing it the obvious way does not work:
first = "A"
second = "B"
first = second
second = first
print(first)
print(second)B
BBoth ended up as "B". Line first = second pointed first at "B", and by the time the next line ran, the original "A" had no name referring to it — so second = first just pointed second at "B" as well.
The traditional fix is a third name to hold one value while the other moves. Python does not need one:
first = "A"
second = "B"
first, second = second, first
print(first)
print(second)B
AWhy it works is exactly the rule from Chapter 2.3 §4: Python works out the entire right-hand side first. At that moment second is "B" and first is "A", so the right-hand side becomes those two values. Only then are the names on the left pointed at them. Neither name has moved while the other was being read.
3. One Value, Several Names
A third form, which chains:
errors = warnings = fatals = 0
print(errors)
print(warnings)
print(fatals)0
0
0All three names now refer to the same value, 0. This is a reasonable way to set several counters to the same starting point.
4. When to Use It
The compact forms are a readability tool, and like any such tool they can be used against the reader.
Reasonable:
test_name, seed = "axi_smoke", 4210Two closely related facts about the same thing, set up together. A reader takes it in at a glance.
Not reasonable:
test_name, seed, run_dir, passed, error_count = "axi_smoke", 4210, "runs/smoke", False, 0Legal, and considerably harder to read than five ordinary lines. To check what run_dir is, you have to count positions on both sides and hope you counted right. A reviewer will not.
The practical rule: two, occasionally three. Past that, use separate lines. The line is doing you no favours once you have to count.
5. Predict, Run, Observe
Predict all output before running.
(a)
a, b = 1, 2
print(a)
print(b)(b)
a, b = 1, 2
a, b = b, a
print(a)
print(b)(c)
x = y = 5
x = 9
print(x)
print(y)(a) 1, 2 (b) 2, 1 (c) 9, 5For (c), check your reasoning: x = 9 pointed x at a new value. y was never mentioned on that line, so it still refers to 5 — the same independence Chapter 2.3 established.
6. Exercise
Exercise 1 — set up a run. Using one line, give names to a test name and a seed. Print both. Then add a third fact on a separate line and say in one sentence why you did not extend the first line to three.
Exercise 2 — swap and explain. Write the swap from §2. Then explain in two sentences why the three-line version fails, using the phrase "right-hand side".
Exercise 3 — predict the error. Predict the exact error, then run it:
a, b, c = 1, 2What does the number in the message refer to — the names or the values?
Exercise 4 — judge the line. Which of these would you accept in a code review, and why?
low, high = 0, 100
a, b, c, d = 1, "two", True, None
first = second = third = 07. Summary
- Names on the left, values on the right, matched in order. The counts must agree or Python raises a
ValueError. a, b = b, aswaps two values with no temporary name, because the whole right-hand side is worked out before anything is assigned.- The naive three-line swap fails, and understanding why is a good test of the model from Chapter 2.3.
x = y = z = 0points several names at one value — safe for every value type in this module.- Two or three related values, at most. Past that the line stops helping the reader and starts costing them.
Assignment in all its forms is now covered. The remaining chapters of Module 2 are about the values themselves — starting with the two kinds of number.
Where this fits
Part of the Python curriculum.
