What is it?
Most of us are taught that a variable is a box with a name on it, and that assignment puts a value in the box. In Python that model is wrong, and it will eventually cost you an afternoon.
A Python variable is a name bound to an object. The object exists somewhere in memory on its own. The name is a label pointing at it. Assignment does not copy a value into a container - it moves a label.
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4]
print(a is b) # True - one list, two names
Nothing was copied. a and b are two labels on the same list object.
Why does it exist?
Because in Python everything is an object - integers, functions, classes, modules - and objects are heap-allocated and reference-counted. A name is just an entry in a namespace dictionary mapping a string to a pointer.
You can see the namespace directly:
x = 42
print(globals()["x"]) # 42
That is all a module-level variable is: a key in a dict. Which is why del x removes the name and not necessarily the object - the object survives as long as something else still points at it.
How does it work?
Two operations look similar and are not:
Rebinding points the name at a different object. The old object is untouched.
Mutation changes the object itself, and every name pointing at it sees the change.
a = [1, 2, 3]
b = a
a = a + [4] # rebinding: builds a NEW list, points `a` at it
print(a, b) # [1, 2, 3, 4] [1, 2, 3]
a = [1, 2, 3]
b = a
a += [4] # mutation: list.__iadd__ extends in place
print(a, b) # [1, 2, 3, 4] [1, 2, 3, 4]
a = a + [4] and a += [4] are the same for immutable types and genuinely different for mutable ones. That asymmetry is not a wart - it falls straight out of the name-binding model.
The same model explains the argument-passing question people argue about endlessly. Python is neither “pass by value” nor “pass by reference”: it passes the binding. The function gets its own name pointing at your object. Rebind it inside and you changed nothing outside; mutate it and you changed the caller’s object.
def rebind(items):
items = items + [99] # local name now points elsewhere
def mutate(items):
items.append(99) # the caller's list changes
And it explains the classic interview trap:
def add(item, bucket=[]): # the default list is created ONCE, at def time
bucket.append(item)
return bucket
add(1) # [1]
add(2) # [1, 2] - same list, still there
Where is it actually used?
Three places this shows up in real work:
- Mutable default arguments - the bug above, still shipping in production code everywhere. Use
Noneas the default and build the object inside. - Shallow copies.
list(a),a[:]andcopy.copy(a)all copy the outer container and keep the inner objects shared. A list of lists copied shallowly still aliases its rows. Usecopy.deepcopywhen you actually mean it. - NumPy and pandas slicing. A NumPy slice is a view, not a copy - writing to it writes through to the base array. Half of all “my dataframe changed and I do not know why” bugs are this model applied one level up.
import numpy as np
base = np.arange(5)
view = base[1:4]
view[0] = 99
print(base) # [ 0 99 2 3 4]
Once you stop imagining boxes, none of these are surprising any more. They are all the same fact: names point at objects, and some objects can be changed underneath their names.
Going deeper
id()andiscompare identity, not equality - trya = 256; b = 256; a is band then again with257. CPython caches small integers, which is an implementation detail you should never rely on but should know exists.- The
dismodule will show you thata = 1compiles toSTORE_NAME, not to anything resembling a write into a box.