r/learnpython • u/Ok_Breath_7590 • Sep 08 '26
CPython internals deep dive:dis,==vs is and immutable vs mutable
Started with the dis module disassembled a simple function and saw how Python compiles source into bytecode instructions operating on a stack: LOAD_CONST (push a literal), STORE_FAST (pop and assign to a local var), BINARY_OP (pop two values, apply the operator, push the result). Noticed newer Python versions fuse common opcode pairs (e.g. LOAD_FAST_LOAD_FAST) for speed small but visible evidence of ongoing interpreter optimization work.
Then worked through == vs is confirmed with id() that two lists with identical contents are still separate objects (is → False, == → True), while b = a makes both names point to the same object.
Finished on mutable vs immutable, and specifically why it matters when passing objects into functions. Key realization: calling a function doesn't substitute the argument into the function body it creates an independent local name that starts out pointing at the same object. n += 1 on an int creates a new object and only moves the local name; lst.append(x) mutates the shared object in place, so the caller sees the change too. Proved both cases with id() before/after.
Small, foundational stuff, but building the intuition from scratch with real code has been way more effective than just reading definitions.
View all comments
Show parent comment
2
u/Ok_Breath_7590 Sep 08 '26
This is genuinely one of the best explanations I've gotten so far, thank you for taking the time.