Module 13: Python under the hood
Objects and identity, copies, memory management, how list and dict really work, and the GIL.
- Everything is an object: Names are labels on objects; == compares values, is compares identity (use it only for None); functions get a new label on the same object.
- Copies: assignment, shallow and deep: b = a copies nothing; copy.copy/.copy()/[:] copy only the outer object; copy.deepcopy copies everything inside too.
- Memory management: CPython frees an object when its reference count hits 0; the gc collector frees reference cycles; weakref points without keeping alive.
- How list, dict and set work: A list is a growing array (index O(1), in O(n), pop(0) O(n)); dict and set are hash tables (in O(1)) that need hashable keys.
- Bytecode, the GIL and the Python way: CPython compiles to bytecode and runs it in a VM; the GIL lets one thread run bytecode at a time, so threads help I/O, not CPU work.
Ends with a 12-question checkpoint (80% passes) and a mini project: Memory detective.