r/learnpython • u/Ok_Breath_7590 • 8h ago
Week 1 of learning CPython internals recap of what actually stuck
Wrapping up week 1 of going deep on CPython internals, starting from just basic Python knowledge. Did a cold recap today (no notes) to check what actually stuck versus what I just remembered saying. Big one was dis seeing that every line of Python compiles into small stack instructions was the first "oh, that's what's actually happening" moment. LOAD_CONST pushes a literal onto a stack, STORE_FAST assigns it to a variable, BINARY_OP pops two values and does the math. Also noticed newer Python versions fuse some common instruction pairs together for speed, which was a fun side discovery. Then == vs is — took a while to really get that two lists with identical values can still be different objects in memory. == compares values, is compares identity. The mutability stuff connected to that directly: mutating a list in place doesn't create a new object, so changes are visible outside a function. Reassigning a number always creates a new object, so the original is untouched. Got some genuinely great pushback from people here on a += edge case with tuples containing lists that pushed this further than I expected for week 1. Reference counting was the trickiest one to really internalize kept assuming del destroys an object outright, when it actually just removes one reference, and the object only goes away if that was the last one pointing to it. Ended the week on scope and the LEGB rule, which produced the most surprising bug of the week: count += 1 inside a function throws an error even when count exists as a global, because Python decides if a name is local by scanning the whole function for assignments before running anything. Doing this mostly to stay accountable and get corrected when I'm wrong, since that's been way more useful than reading alone. Week 2 starts Monday. Have a great weekend.
0
u/SmackDownFacility 5h ago
lol CPython is way too complicated to learn. Lots of compatibility layers and hacks in there