Skip to content
VLSI Mentor

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.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
total = 10
errors = total
total = 99
print(errors)

Most people predict 99. The answer is:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
10

If 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.

Three stages. After line one, the name total refers to the value 10. After line two, both total and errors refer to that same value 10. After line three, total refers to a new value 99 while errors still refers to 10.totalafter line 110total · errorsafter line 210totalafter line 39912
Figure 1 — the state after each line. Line 2 gives errors the value total referred to at that moment; line 3 moves total alone. Nothing joins the two names together, so moving one has no effect on the other.

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.

Azvya Education Pvt. Ltd.VLSI Mentor
NOT PYTHON — a continuous assignment in Verilog
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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
count = 1
count = count + 1
print(count)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
2

As a mathematical statement, count = count + 1 is false for every possible value. As an instruction it is perfectly ordinary:

  1. Work out the right-hand side. count currently refers to 1, so count + 1 is 2.
  2. Point count at 2.

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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
name = "running"
name = "complete"
print(name)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
complete

The 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)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
seed = 4210
backup = seed
seed = 9999
print(seed)
print(backup)

(b)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a = "first"
b = a
a = "second"
b = a
print(a)
print(b)

(c)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
x = 1
y = x
x = 2
y = 3
print(x)
print(y)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
(a)  9999      (b)  second      (c)  2
     4210           second           3

For (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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
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 = a points b at whatever a refers 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 + 1 is 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.