Python Part I · Module 2 · Variables and Data Types
What `=` Really Means
The previous chapter introduced assignment and said it happens once, when the line runs. This chapter is about what follows from that, because the consequences are not obvious and getting them wrong produces bugs that are genuinely hard to see. The central experiment takes four lines: point one name at a value, point a second name at the first, change the first, and then ask what the second refers to. Most people's first instinct is wrong, and the instinct is wrong in a specific way that comes from thinking of assignment as a connection between two things rather than an action performed at a moment in time. If you have written Verilog or SystemVerilog, that instinct is especially strong and this chapter is written with you in mind.
Foundation11 min readPythonPart IFundamentalsAssignmentMental Model
Part I · Module 2 · Chapter 2.3 · What = Really Means
1. The Experiment
Read these four lines and predict the output before running them. Write your prediction down — this is worth being wrong about on paper rather than in a script.
total = 10
errors = total
total = 99
print(errors)Most people predict 99. The answer is:
10If you predicted 99, the rest of this chapter is the reason why, and it is worth the ten minutes.
2. What Each Line Actually Did
Take them one at a time, using the name/value model from Chapter 2.2.
Line 1 — total = 10. The value 10 exists. The name total now refers to it.
Line 2 — errors = total. Python evaluates the right-hand side first. What is total right now? It refers to 10. So errors is pointed at the same value, 10. There are now two names and one value.
Line 3 — total = 99. A new value, 99, and total is pointed at it instead. Nothing was said about errors on this line, so errors was not touched. It still refers to 10.
Line 4 — print(errors). errors refers to 10. So 10.
3. Why the Wrong Answer Feels Right
The prediction of 99 comes from reading line 2 as though it created a link — as though errors were now defined in terms of total, so that changing total would flow through.
That reading is not unreasonable. It is exactly how some other things work. It is just not how Python's = works.
If you write RTL
This is where the instinct is strongest, and it is worth being explicit about.
assign errors = total;That line describes a permanent relationship. It is not an action that happens once; it is a statement about the circuit that remains true for as long as the design exists. Change total and errors follows, because they are wired together.
Python's = does none of that. There is no wire, nothing re-evaluates, and nothing follows. The line runs, the name is pointed somewhere, and Python moves on to the next line.
If you come from mathematics
A second instinct, equally reasonable and equally wrong. In mathematics = asserts that two things are the same, and such an assertion does not change over time.
In Python it is an instruction. Which is why the next section is possible at all.
4. Right-Hand Side First
One rule makes every assignment predictable:
Python works out the entire right-hand side first, and only then points the name on the left at the result.
That explains something which looks like nonsense if you read = as equality:
count = 1
count = count + 1
print(count)2As a mathematical statement, count = count + 1 is false for every possible value. As an instruction it is perfectly ordinary:
- Work out the right-hand side.
countcurrently refers to1, socount + 1is2. - Point
countat2.
The old value is not modified — count simply refers to a different one now.
5. Rebinding Does Not Change Values
Worth stating plainly, because it is the same point from a different angle:
name = "running"
name = "complete"
print(name)completeThe text "running" was not edited. No characters were overwritten. The name name stopped referring to "running" and started referring to "complete".
This is why the model matters. If you picture a box, you picture its contents being replaced — and you will predict wrongly in Module 5 when you meet values that genuinely can be modified in place. Keeping the name was pointed somewhere else as your model means you will still be right then.
Chapter 2.13 is where that distinction gets made properly.
6. Predict, Run, Observe
Three experiments. Predict each before running, and write the prediction down.
(a)
seed = 4210
backup = seed
seed = 9999
print(seed)
print(backup)(b)
a = "first"
b = a
a = "second"
b = a
print(a)
print(b)(c)
x = 1
y = x
x = 2
y = 3
print(x)
print(y)(a) 9999 (b) second (c) 2
4210 second 3For (b), notice the second b = a happened after a had already moved, so b was pointed at the new value. Each assignment uses whatever the right-hand side refers to at that instant, and no earlier.
7. Exercise
Exercise 1 — explain the surprise. A colleague writes the four lines from §1, expects 99, and gets 10. Explain what happened in two sentences without using the words "box", "copy" or "store".
Exercise 2 — make it print 99. Change the program in §1 so that it prints 99, by adding or moving exactly one line. There is more than one correct answer — state which one you chose and why.
Exercise 3 — order matters. Predict the output, then run it:
count = 5
count = 10
count = 3
print(count)Then answer: how many values were created, and how many does count refer to at the end?
Exercise 4 — the RTL question. If you have written Verilog: in one sentence each, say what assign errors = total; guarantees over time, and what errors = total in Python guarantees over time. The difference between those two sentences is the whole chapter.
8. Summary
- Assignment happens once, at the moment its line runs. It is an action, not a lasting relationship.
b = apointsbat whateverarefers to right then. The two names are independent afterwards; moving one does not move the other.- Python evaluates the right-hand side first, then points the left-hand name at the result — which is why
count = count + 1is ordinary rather than absurd. - Reassignment never modifies the old value. The name stops referring to it; the value itself is untouched.
- Python's
=is not a continuous assignment, not a connection, and not a mathematical equation. For RTL engineers this is the most expensive habit to carry over.
With assignment properly understood, the remaining mechanics of naming are quick. The next chapter covers what Python actually permits in a name — and what it rejects.
Where this fits
Part of the Python curriculum.
