Variables and objects in Python: it's pointers all the way down
summary
Demonstrates five gotchas about Python references: assignment never copies, mutations change shared objects, function arguments are just bindings so in-place updates modify callers, self‑concatenating lists and dict.fromkeys create multiple references to one list (dict.fromkeys bug), and default mutable arguments leak state (default mutable arg). Shows failing code and fixes: use list/dict comprehensions to create distinct objects, or copy with list() in __init__; explains identity vs equality and when to return a new object versus mutating in place.
AI-written summary · about
chapters (13)
- 0:00Python mental models
- 0:51Python gotchas explained
- 3:54Variables as pointers
- 6:12Mutations vs assignments
- 8:32Equality vs identity
- 10:12Python assignment features
- 12:57Object references
- 15:15Fixing list of lists gotcha
- 17:14Fixing dict.fromkeys gotcha
- 18:23Fixing to-do list class gotcha
- 21:19Recap of variables and objects
- 24:07Why Python works this way
- 26:14Closing thoughts
related talks
23mPython Memory Understanding
27mPython Oddities Explained
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
49mSeven Things Every Python Programmer Should Know
47mA Deep-Dive Into Python Classes
02h 26mPointers in Python
25mIs it me or Python memory management? 🤔💻
