Under the Hood Hacking Python Data Types for Fun and Power
summary
Shows how to make a Python list usable as a dict key by implementing __hash__ and __eq__ (example uses an MD5-based integer hash), how to make sets indexable and sliceable via __getitem__ (including using date objects as indices), how to mutate frozen dataclasses with object.__setattr__ and even alter tuple contents by writing into C-level memory, and how generators act as coroutines via generator.send() (used to build a scheduler and a rolling average). Notes the power and the serious risk of corrupting invariants.
AI-written summary · about
chapters (14)
- 0:00Introduction
- 1:12Python data types basics
- 3:42Everything is an object
- 6:38Dunder methods backbone
- 8:25Custom data types
- 11:09Hashable lists for keys
- 13:21Indexable and slicable sets
- 16:34Non-integer key indexing
- 18:26Mutating frozen data classes
- 20:11Mutating tuples memory
- 22:00Generators and async basics
- 24:51Rolling average generator
- 25:36Hashability and dunder lessons
- 26:20Immutability contract
related talks
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
24m"A Confession of My Python Sins"
01h 07mWhat Does It All Really Mean
25mAdvanced Python
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
52mPython Tips, Tricks, and Idioms
31mPlaying with CPython Objects Internals
27mPython Oddities Explained
