📖 Python Runtime Internals
Object Model
- PyObject: Everything is an object (header + data)
- Reference counting: Immediate cleanup, deterministic
- Type object: Methods, attributes stored on type
Reference Counting
- Increment: Assignment, function argument, container add
- Decrement: Reassignment, del, scope exit
- Zero count: Immediate deallocation
- Weakness: Circular references
Cycle-Detecting GC
- Generational: Gen 0, 1, 2 (young → old)
- Only containers: Lists, dicts, objects (not ints, strings)
- Threshold-based: Triggers when allocation exceeds threshold
Global Interpreter Lock (GIL)
What It Is
- Mutex protecting Python object access
- Only one thread executes Python bytecode at a time