Dipping my toes in metaprogramming
summary
Shows how Python's __new__ returns arbitrary objects and how it differs from __init__; implements a Number class whose __new__ swaps cls for OddNumber or EvenNumber so instantiating Number(3) yields an OddNumber, and reproduces pathlib's OS-based Path swap for Windows/POSIX via cls replacement. Demonstrates printing inside __new__ to prove it runs, uses super().__new__(cls, ...) to allocate the object, builds a tolerant float subclass that must use __new__ because floats are immutable, and all live demos work.
AI-written summary · about
chapters (7)
- 0:00Introduction and motivation
- 3:18Emulating path with numbers
- 10:33Understanding __new__ method
- 16:18Dynamic class switching
- 20:34Path implementation details
- 27:24Immutability and floats
- 31:41Meta classes and conclusion
related talks
05mThe Craziest fn Thing I've Ever Seen (and It Lives in CPython!!!)
52mMetaprogramming With The Groovy Compiler - Part 2 of 2
27mVariables and objects in Python: it's pointers all the way down
50mExploring CPython's bytecode
01h 00mModern Template Metaprogramming: A Compendium, Part I
22mLow Level Python a Deep dive into the python sources
21mBuilding Ruby in Ruby
07mProgramming Languages Made Better
