CS Fundamentals Trainer · Guides
How Python works
Names as labels on objects, mutability and the mutable default argument, the data model behind operators and loops, generators (1.4 KB against 409 MB), closures and late binding, reference counting and the cycle collector, __slots__, and measured costs.
23 min read
Most of Python's surprises come from one idea that is easy to state and easy to forget: a name is a label stuck on an object, not a box that holds a value. The mutable default argument, the list of lists that changes all its rows at once, the lambdas that all return the same number, the object that is still alive after you deleted it: each one follows directly from how names, objects, scopes and memory work. This guide explains that machinery in order and runs a small experiment for every claim. The experiments were run on CPython 3.13 (the standard implementation, which is what "Python" means below); the scripts are in the appendix, and timings and sizes come from one small machine, so the behaviour is what to take away, not the digits.
1. What runs your code
Python is neither "interpreted" nor "compiled" in the simple sense. CPython does both steps:
Diagram: From source file to running code in CPython
The source is compiled to bytecode, a compact list of instructions for a stack-based virtual machine, and the VM executes it one instruction at a time. Imported modules have their bytecode cached in __pycache__/*.pyc, so the compile step is skipped next time. You can look at the bytecode with the dis module. For def f(a, b): return a + b on Python 3.13:
RESUME 0
LOAD_FAST_LOAD_FAST 1 (a, b)
BINARY_OP 0 (+)
RETURN_VALUE
Two things follow. First, + is not a CPU addition: the VM looks at the types of the two operands at run time and asks the left one how to add itself to the right one (section 4). That flexibility is why Python is pleasant, and also why a loop over numbers is much slower than the same loop in a compiled language. Second, the exact bytecode changes from one Python version to the next (the output above is from 3.13), so treat it as an explanation, not as an interface.
2. Names, objects and mutability
Every value in Python is an object: it has an identity (id(), its address in CPython), a type, and a value. A variable is a name bound to an object. Assignment never copies the object; it makes the name refer to it.
Diagram: Two names, one object
I ran this:
a = [1, 2, 3]
b = a # a second name for the same list
b.append(4)
print(a) # [1, 2, 3, 4] a changed, because there is only one list
c = list(a) # a new list with the same items
c.append(5)
print(a, c) # [1, 2, 3, 4] [1, 2, 3, 4, 5]
a is b was True and a is c was False. is asks "the same object?" and == asks "equal values?": two separate lists [1] and [1] were == but not is. Use is for None (x is None) and for nothing else. It can also mislead you: CPython keeps one shared object for each small integer from -5 to 256, so int("256") is int("256") was True and int("257") is int("257") was False. That is an implementation detail, not something to rely on.
Mutable and immutable
Some objects can change in place (mutable: list, dict, set, most classes) and some cannot (immutable: int, float, str, tuple, frozenset, bytes). With immutable objects, "changing" a variable binds the name to a new object. The same operator can behave differently on the two kinds:
| Operation | What happened |
|---|---|
t = (1, 2); t += (3,) | a new tuple was created (the id changed) |
l = [1, 2]; l += [3] | the same list was extended in place (the id did not change) |
n = 10; n += 1 | a new int object |
This is why passing a list to a function lets the function change the caller's list, while passing a string never does. Python passes references to objects by value (sometimes called "call by sharing"): the function gets a new name for the same object.
The mutable default argument
Default values are evaluated once, when the def statement runs, and stored on the function object. If the default is mutable, every call that omits the argument shares it:
def add(item, bucket=[]):
bucket.append(item)
return bucket
add(1) # [1]
add(2) # [1, 2] the same list as before
add(3) # [1, 2, 3]
add.__defaults__ # ([1, 2, 3],)
The list lives in add.__defaults__. The standard fix uses None as a marker and creates the list inside the function (if bucket is None: bucket = []); two calls then returned [1] and [2].
Shared rows and shallow copies
[[0] * 3] * 3 does not build three rows; it builds one row and three references to it. After grid[0][0] = 9 the result was [[9, 0, 0], [9, 0, 0], [9, 0, 0]]. A comprehension, [[0] * 3 for _ in range(3)], evaluates the inner expression for each row and gave [[9, 0, 0], [0, 0, 0], [0, 0, 0]].
The same idea explains shallow and deep copies. copy.copy (and list(x), x[:], dict(x)) copies the outer container but shares the objects inside; copy.deepcopy copies recursively. With orig = {"tags": ["a"], "n": 1}, after orig["tags"].append("b") the shallow copy showed {'tags': ['a', 'b'], 'n': 1} and the deep copy {'tags': ['a'], 'n': 1}.
A related trap lives in classes: an attribute defined in the class body (items = []) belongs to the class and is shared by every instance. After a1.items.append(1), a second instance's items was also [1]. Create per-instance state in __init__.
3. The data model: how syntax maps to methods
Python's operators and statements are thin syntax over special ("dunder") methods. The language asks the object to do the work, so your own classes can behave like built-in ones.
| You write | Python calls |
|---|---|
a + b | a.__add__(b) (and tries b.__radd__(a) if that is not supported) |
a == b | a.__eq__(b) |
len(x) | x.__len__() |
bool(x), if x: | x.__bool__(), or __len__() if there is no __bool__ |
for v in x: | iter(x), then next(...) repeatedly until StopIteration |
with x as r: | x.__enter__(), then x.__exit__(...) however the block ends |
d[key] | d.__getitem__(key) |
repr(x) | x.__repr__() |
I wrote a small Money class with __add__, __eq__, __hash__, __repr__, __len__ and __bool__: Money(5) + Money(7) printed Money(12), Money(5) == Money(5) was True, and bool(Money(0)) was False.
Equality and hashing go together
A dict or set finds an item by its hash, then confirms with ==. The rule: objects that compare equal must have the same hash, and an object's hash must not change while it is in a dict or set. That is why mutable containers are unhashable: {[1]: 1} raised TypeError: unhashable type: 'list'. If you define __eq__ on a class, Python sets __hash__ to None unless you define it too, so instances stop being usable as keys. Define both, based on the same immutable fields (Money above does).
The hash is also why lookups are fast. Testing membership of the last of 100,000 numbers took about 700 microseconds in a list (a scan that compares one item after another) and about 50 nanoseconds in a set (hash, jump to the slot): a ratio in the thousands. The cost of x in some_list grows with the list; the cost of x in some_set does not.
Iterators, generators and laziness
A for loop is the iterator protocol: it calls iter() on the object and then next() until StopIteration. I wrote a Countdown class with __iter__ and __next__, and list(Countdown(3)) gave [3, 2, 1]. A generator function (one that uses yield) builds such an iterator for you, and runs only when asked for the next value. Two properties matter:
- It is lazy. The values are produced one at a time and never stored together.
- It is one-shot. After
g = squares(5),list(g)gave[0, 1, 4, 9, 16]and a secondlist(g)gave[]: the generator was exhausted.
Laziness is a memory optimisation. I computed the sum of the squares of 10 million integers two ways and measured peak allocation with tracemalloc:
| Approach | Peak memory |
|---|---|
sum([i * i for i in range(10_000_000)]) (build the list first) | about 409 MB |
sum(i * i for i in range(10_000_000)) (generator expression) | about 1.4 KB |
(For the generator, sys.getsizeof itself reported 200 bytes.) Use a list when you need to index, reuse or take the length of the data; use a generator when you only pass over it once.
Context managers
with guarantees the exit code runs however the block ends. A class with __enter__ and __exit__ printed enter, then exit ValueError when the body raised, and the exception then propagated, because __exit__ returned False. Returning a true value from __exit__ would swallow the exception, so do that only on purpose. For files, locks and database connections, prefer with to a manual close() that an exception can skip. contextlib.contextmanager turns a generator into a context manager when writing a class is too much.
4. Scope and closures
Python decides at compile time whether a name is local to a function: if the function assigns to it anywhere, it is local everywhere in that function. Lookups follow the LEGB order: Local, Enclosing function, Global (module), Built-in.
x = "global"
def bad():
print(x) # looks for a local x, because x is assigned below
x = "local"
bad()
# UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
To rebind a name from an outer function you need nonlocal (for a global, global). Reading an outer name needs nothing. A function that remembers the variables of the function that created it is a closure; I made a counter with nonlocal n and three calls gave 3, and c.__closure__[0].cell_contents was also 3: the variable lives in a shared "cell", not in a copy.
Late binding
A closure captures the variable, not its value at the time. So this prints the last value for every function:
fs = [lambda: i for i in range(3)]
[f() for f in fs] # [2, 2, 2] all three see the same i, now 2
fs = [lambda i=i: i for i in range(3)]
[f() for f in fs] # [0, 1, 2] i=i binds the value when each lambda is made
The default argument i=i is evaluated at definition time (the same rule as the mutable default above), which is why it freezes the value. functools.partial does the same job more readably.
5. Memory: reference counting and the cycle collector
CPython frees objects with two cooperating mechanisms.
Reference counting is the primary one. Each object counts how many references point at it. sys.getrefcount of a fresh list (minus the temporary reference the call itself makes) was 1; after w = v it was 2; after del w it was 1 again. When the count reaches zero, the object is freed immediately. That is why a file object that goes out of scope is closed promptly in CPython and why destructors are predictable, though you should not depend on it: other Python implementations do not behave this way, and an exception traceback can keep objects alive.
The cyclic garbage collector handles what counting cannot: objects that refer to each other keep each other's counts above zero. I made two Node objects that point at each other and deleted both names. A weak reference to one still found it alive. After gc.collect() it was gone. An object that is not part of a cycle was freed the moment its last name was deleted. The collector runs automatically, based on counts of container allocations (the default threshold tuple on 3.13 printed (2000, 10, 10)); you rarely need to call it. For the details of tracing collectors in general, see the garbage collection guide.
Use a weak reference (weakref) when you want to refer to an object without keeping it alive, for example in a cache.
How big is an object?
Everything is an object, so small things are bigger than you might expect. sys.getsizeof on 3.13:
| Object | Bytes |
|---|---|
0 or 1 (an int) | 28 |
2**100 | 40 |
"" (empty string) | 41 |
"hello" | 46 |
[] | 56 |
[1, 2, 3] | 88 |
(1, 2, 3) | 64 |
{} | 64 |
set() | 216 |
A list stores pointers, so a list of ten million ints costs far more than ten million times 8 bytes: that is where most of the 409 MB above went. getsizeof does not include the objects a container refers to.
For classes with many instances, a per-instance dictionary adds up. Declaring __slots__ = ("a", "b") removes it. One million instances with two attributes took about 96 MB as a plain class and about 56 MB with __slots__, measured with tracemalloc. The price: no new attributes can be added, and some features (such as weak references or multiple inheritance) need extra care. Reach for it when you really have millions of small objects; for ordinary code the plain class is fine.
6. Performance: what the machinery implies
- Pick the right data structure. The list-versus-set difference above (thousands of times for a 100,000-item container) matters more than any micro-optimisation. See also the memory hierarchy and Big O in practice.
Build strings with join, but know the real rule. The folklore says s += "x" in a loop is quadratic. In CPython it often is not: when the string has no other reference, the interpreter extends it in place. I measured three versions:
| Length | s += "x" | += while a second name holds the old string | "".join(...) |
|---|---|---|---|
| 10,000 | 0.5 ms | 1.2 ms | 0.1 ms |
| 100,000 | 3.9 ms | 187 ms | 1.3 ms |
| 200,000 | 15.4 ms | 587 ms | 2.6 ms |
The in-place trick is an optimisation of one implementation and disappears when any other reference exists (doubling the length made the second column about three times slower here, as copying took over). join is fast everywhere, so use it when building a string from many pieces.
Two more habits:
- Move work out of the interpreter loop. Built-ins (
sum,sorted,min,str.join) and library code written in C run as native code, and comprehensions avoid some of the overhead of an explicit loop. A loop that does a little Python work per item pays the bytecode cost on every iteration. - Measure. Use
timeitfor small snippets,cProfilefor programs andtracemallocfor memory. Intuition about what is slow in Python is often wrong, as the+=result shows.
Threads and the GIL, in one paragraph
In the standard build, CPython has a global interpreter lock (GIL): only one thread runs Python bytecode at a time. Threads still help for I/O (a thread waiting on the network releases the lock), but CPU-bound Python code does not run faster on more threads; use processes (multiprocessing, concurrent.futures.ProcessPoolExecutor) or native extensions for that. Python 3.13 added an experimental free-threaded build without the GIL (PEP 703); the build I ran reported the GIL as enabled, as most installations do. For races, deadlocks and asyncio, see the concurrency guide.
7. Common mistakes
- Mutable default arguments (
def f(x=[]),def f(x={})). UseNoneand create the object inside. [[0] * n] * mfor a grid. Use a comprehension.- Expecting
=to copy. It adds a name. Copy explicitly, and remember thatcopy.copyis shallow. - Using
isto compare values. Use==; keepisforNone. - Defining
__eq__without__hash__, then wondering why instances cannot be dict keys. - Lambdas or inner functions created in a loop that all see the last loop value.
- Testing membership in a big list inside a loop. Build a set once.
- Consuming a generator twice, and getting nothing the second time.
- Relying on prompt destruction (
__del__, a file closing when it goes out of scope). Usewith. - Building a string with
+=in a loop because it happened to be fast in a test. Usejoin.
8. Questions interviewers like to ask
"Is Python pass-by-value or pass-by-reference?"
Neither exactly. The function receives a new name bound to the same object (call by sharing). Mutating the object is visible to the caller; rebinding the parameter is not.
"Why does def f(x=[]) misbehave?"
The default is evaluated once when the function is defined and stored on the function, so all calls without the argument share one list. Use None as the default and create the list inside.
"What is the difference between is and ==?"
is compares identity (same object), == compares value through __eq__. Use is only for singletons such as None.
"How does Python manage memory?"
Reference counting frees most objects immediately when the last reference goes away; a cyclic garbage collector finds groups of objects that only refer to each other. weakref references do not keep objects alive.
"What is a generator, and why use one?"
A function that yields produces values lazily, one at a time. It uses constant memory (1.4 KB against 409 MB in the sum above) but can be consumed only once.
"Why are lists not hashable?"
A hash must not change while the object is in a dict or set, and equal objects must hash equally. A mutable list can change, so its hash would be unstable. Tuples of hashable items are hashable.
"What does the GIL do?"
It lets only one thread execute Python bytecode at a time in the standard CPython build. I/O-bound threads still overlap; CPU-bound work needs processes or native code.
9. Things to mention in an interview
- Names refer to objects; assignment binds, it does not copy; mutable versus immutable.
- Shallow versus deep copy; the mutable default argument; late-binding closures.
- The data model: operators and statements call dunder methods;
__eq__and__hash__together; the iterator protocol;withand__exit__. - LEGB,
nonlocal, and whyUnboundLocalErrorhappens. - Reference counting plus the cycle collector; weak references;
__slots__and the real size of objects. - Choosing data structures (set versus list) and the string-building story: measure before believing folklore.
- The GIL and what it means for threads, processes and
asyncio.
Quick recap
- A name is a label on an object.
b = aadds a label;a is bisTrueand a change through one is seen through the other. - Immutable objects are replaced when you "change" them; mutable ones change in place, and the same
+=does different things (tuplenew object,listsame object). - Defaults are evaluated once:
def add(item, bucket=[])returned[1],[1, 2],[1, 2, 3]from one shared list.[[0] * 3] * 3shares one row. - Operators and statements are syntax over dunder methods; equal objects must hash equally; a set lookup took about 50 ns against about 700 µs for a list scan of 100,000 items.
- Generators are lazy and one-shot: 1.4 KB against 409 MB to sum 10 million squares.
- Closures capture variables, not values:
[2, 2, 2]untili=ifixes the value. - CPython frees objects by reference counting and collects cycles separately;
__slots__cut one million small objects from about 96 MB to about 56 MB. - Measure:
s += "x"was fine until a second reference to the string made it 187 ms instead of 4 ms at 100,000 characters.
Appendix: the experiment code
Python 3.13 (CPython), standard library only. Run each file with python3 file.py. Sizes and timings depend on the machine and on the Python version (the numbers in the guide come from one small machine); the behaviours, such as which names share an object, should be the same on any CPython 3.
names.py: names, aliasing, is against ==, mutable defaults, shared rows, copies and reference counts:
import sys, copy
# 1. names and aliasing
a = [1, 2, 3]
b = a
b.append(4)
print("alias:", a, a is b, id(a) == id(b))
c = list(a)
c.append(5)
print("copy:", a, c, a is c)
# 2. is vs ==
x = 1000; y = 1000
print("== :", x == y)
s1 = [1]; s2 = [1]
print("lists == / is:", s1 == s2, s1 is s2)
print("small ints cached:", (lambda p, q: p is q)(int("5"), int("5")))
# 3. mutable default
def add(item, bucket=[]):
bucket.append(item)
return bucket
print("default:", add(1)); print("default:", add(2)); print("default:", add(3))
print("default object:", add.__defaults__)
def add_ok(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
print("fixed:", add_ok(1), add_ok(2))
# 4. [[0]*3]*3
grid = [[0] * 3] * 3
grid[0][0] = 9
print("shared rows:", grid)
grid = [[0] * 3 for _ in range(3)]
grid[0][0] = 9
print("separate rows:", grid)
# 5. shallow vs deep
orig = {"tags": ["a"], "n": 1}
sh = copy.copy(orig); dp = copy.deepcopy(orig)
orig["tags"].append("b")
print("shallow:", sh, "deep:", dp)
# 6. immutable "changes" create objects; += on list vs tuple
t = (1, 2); id0 = id(t); t += (3,)
print("tuple += new object:", id(t) != id0, t)
l = [1, 2]; id0 = id(l); l += [3]
print("list += same object:", id(l) == id0, l)
n = 10; id0 = id(n); n += 1
print("int += new object:", id(n) != id0)
# 7. refcount
v = []
print("refcount:", sys.getrefcount(v) - 1)
w = v; print("refcount after alias:", sys.getrefcount(v) - 1)
del w; print("refcount after del:", sys.getrefcount(v) - 1)
model.py: dunder methods, the iterator protocol, generators, with, and the memory of a list against a generator (it needs a few hundred MB of free memory for the list):
import sys, tracemalloc, dis
# dunder protocol
class Money:
def __init__(self, cents): self.cents = cents
def __add__(self, other): return Money(self.cents + other.cents)
def __eq__(self, other): return isinstance(other, Money) and self.cents == other.cents
def __hash__(self): return hash(self.cents)
def __repr__(self): return f"Money({self.cents})"
def __len__(self): return self.cents
def __bool__(self): return self.cents != 0
print(Money(5) + Money(7), Money(5) == Money(5), len(Money(5)), bool(Money(0)))
print({Money(1): "a"}[Money(1)])
# for loop is iterator protocol
class Countdown:
def __init__(self, n): self.n = n
def __iter__(self): return self
def __next__(self):
if self.n == 0: raise StopIteration
self.n -= 1
return self.n + 1
print(list(Countdown(3)))
it = iter([10, 20]); print(next(it), next(it));
try: next(it)
except StopIteration: print("StopIteration")
# generator is lazy and one-shot
def squares(n):
for i in range(n):
yield i * i
g = squares(5)
print(list(g), list(g))
# with = __enter__/__exit__
class Res:
def __enter__(self): print("enter"); return self
def __exit__(self, et, ev, tb): print("exit", et.__name__ if et else None); return False
try:
with Res(): raise ValueError("x")
except ValueError: print("propagated")
# memory: list vs generator for 10 million
N = 10_000_000
tracemalloc.start()
lst = [i * i for i in range(N)]
cur, peak = tracemalloc.get_traced_memory(); print(f"list of {N}: peak {peak/1e6:.0f} MB")
del lst
tracemalloc.reset_peak()
tot = sum(i * i for i in range(N))
cur, peak = tracemalloc.get_traced_memory(); print(f"generator sum: peak {peak/1e3:.1f} KB")
tracemalloc.stop()
print("getsizeof gen:", sys.getsizeof(i*i for i in range(N)), "list:", sys.getsizeof(list(range(1000))))
# bytecode
def f(a, b): return a + b
dis.dis(f)
scope.py: closures, LEGB, class attributes, object sizes, __slots__, reference cycles, set against list lookups and string building:
import gc, sys, weakref, timeit
# late binding
fs = [lambda: i for i in range(3)]
print("late binding:", [f() for f in fs])
fs = [lambda i=i: i for i in range(3)]
print("bound at definition:", [f() for f in fs])
# LEGB + UnboundLocalError
x = "global"
def outer():
x = "enclosing"
def inner():
return x
return inner()
print("LEGB:", outer())
def bad():
print(x)
x = "local"
try: bad()
except UnboundLocalError as e: print("UnboundLocalError:", e)
def counter():
n = 0
def inc():
nonlocal n
n += 1
return n
return inc
c = counter(); c(); c()
print("closure cell:", c(), c.__closure__[0].cell_contents)
# class attr vs instance attr
class A:
items = []
a1, a2 = A(), A()
a1.items.append(1)
print("shared class attr:", a2.items)
# memory of objects
class Plain:
def __init__(self): self.a = 1; self.b = 2
class Slotted:
__slots__ = ("a", "b")
def __init__(self): self.a = 1; self.b = 2
p, s = Plain(), Slotted()
print("sizeof obj / dict:", sys.getsizeof(p), sys.getsizeof(p.__dict__), "slotted:", sys.getsizeof(s))
import tracemalloc
for cls in (Plain, Slotted):
tracemalloc.start()
objs = [cls() for _ in range(1_000_000)]
cur, _ = tracemalloc.get_traced_memory(); tracemalloc.stop()
print(f"{cls.__name__}: 1M objects {cur/1e6:.0f} MB")
del objs
# sizes of builtins
print("sizes:", {k: sys.getsizeof(v) for k, v in dict(int0=0, int1=1, big=2**100, empty_list=[], list3=[1,2,3], empty_dict={}, empty_set=set(), tup=(1,2,3), s="", s5="hello").items()})
# cycles: refcount alone does not free
class Node:
def __init__(self): self.other = None
n1, n2 = Node(), Node(); n1.other = n2; n2.other = n1
r = weakref.ref(n1)
del n1, n2
print("alive after del (cycle):", r() is not None)
print("collected objects:", gc.collect() > 0, "alive now:", r() is not None)
m = Node(); r2 = weakref.ref(m); del m
print("no cycle, freed at once:", r2() is None)
print("gc thresholds:", gc.get_threshold())
# hash / dict / set lookup
N = 100_000
lst = list(range(N)); st = set(lst)
t_l = timeit.timeit(lambda: (N - 1) in lst, number=200) / 200
t_s = timeit.timeit(lambda: (N - 1) in st, number=200000) / 200000
print(f"in list {t_l*1e6:.1f} us, in set {t_s*1e9:.0f} ns, ratio {t_l/t_s:.0f}x")
try: {[1]: 1}
except TypeError as e: print("unhashable:", e)
# string concat
def cat(n):
s = ""
for i in range(n): s += "x"
return s
def join(n): return "".join("x" for _ in range(n))
for n in (10_000, 100_000, 1_000_000):
print(n, f"+= {timeit.timeit(lambda: cat(n), number=3)/3*1e3:.1f} ms", f"join {timeit.timeit(lambda: join(n), number=3)/3*1e3:.1f} ms")
# interning
a = "hello"; b = "hel" + "lo"; print("interned literals:", a is b)
concat.py: why += on strings is fast until it is not:
import timeit
def plain(n):
s = ""
for _ in range(n): s += "x"
return s
def held(n):
s = ""; keep = s
for _ in range(n):
keep = s # a second reference defeats the in-place optimisation
s += "x"
return s
def join(n): return "".join(["x"] * n)
for n in (10_000, 100_000, 200_000):
r = [timeit.timeit(lambda: f(n), number=3) / 3 * 1e3 for f in (plain, held, join)]
print(n, "plain += %.1f ms | += with 2nd reference %.1f ms | join %.1f ms" % tuple(r))
Sources
- Python documentation, The Python Language Reference: Data model. Objects, identity, special methods,
__slots__,__hash__. - Python documentation, Execution model. Naming and binding, scopes,
nonlocal. - Python documentation,
dis: Disassembler for Python bytecode,gc,copy,weakrefandtracemalloc. - Python documentation, Programming FAQ. Default arguments, late binding in lambdas, how arguments are passed.
- Ned Batchelder, Facts and myths about Python names and values.
- Python Enhancement Proposals, PEP 3104: Access to names in outer scopes (
nonlocal) and PEP 703: Making the Global Interpreter Lock optional in CPython.
Practise this topic: Python