What You'll Master Here
A name is a label tied to an object. Assignment moves the label. Mutation changes the object every label is tied to.
Every other chapter in this knowledge base tells you what to build. This one tells you what Python is actually doing while you build it — because when a pipeline produces a number nobody can explain, the cause is almost always something in here.
A name in Python is not a box that holds a value. It is a label tied to an object, and two labels can be tied to the same object. Once you internalise that one sentence, a whole family of bugs stops being mysterious: the config that changed after you passed it to a function, the default argument that remembered last week's rows, the copy that was not a copy, the deduplicated set that let a duplicate through.
This is also the highest-density interview chapter in the Python track. "What does this print" rounds are built almost entirely from the material below, and so is the follow-up question after every one of them: why.
A name is a label tied to an object. Assignment moves the label. Mutation changes the object every label is tied to.
These are not trivia. Every one of them fails silently: no exception, no warning, a plausible number, and a bug that survives code review because the code looks right.
- object
- The actual thing in memory — a list, a dict, an int. It has an identity, a type, and a value.
- name
- A label bound to an object. Assignment rebinds the label; it does not copy the object.
- mutable
- The object can be changed in place: list, dict, set, and most classes you write.
- immutable
- The object cannot be changed: int, float, str, bytes, tuple, frozenset.
- identity
- Which object this is, tested with `is`. Distinct from equality, tested with `==`.
Treating a variable as a container that holds a value, rather than a label attached to an object. Every aliasing and copying bug in this chapter follows from that one picture being wrong, and none of them raise an error.
Ask "how many objects are there?" before asking "what are the values?".
Assume a function that takes a dict or a list can change it, unless you can see that it does not.
Treat a mutable default argument as a bug on sight, without needing to reason about it.
Every example is short enough to run in your head first. Predict the output, then read the result table. The gap between your prediction and the result is exactly the thing worth learning.
An output-prediction round is a data-model round wearing a disguise. Every question in it is "which object is this name pointing at, and did something change it?"
Names label objects; they do not contain them. Most Python surprises are two labels on one object, or one label moved when you expected the object to change.
