The Zen of Polymorphism: Choosing between isinstance(), methods, and @singledispatch
summary
Compares three concrete implementations on a calculator AST: naive type switches with isinstance(), methods on classes, and functools.singledispatch; claims singledispatch gives late registration and decoupling while avoiding duplication, then shows a catamorphism (visitor) that factors traversal from operations so pretty‑print, eval, complexity, and type checks reuse the same walk. Builds a working calculator and a tiny custom dispatcher that uses the class MRO and shows the approaches trade performance for maintainability.
AI-written summary · about
chapters (8)
- 0:00Introduction
- 1:09Calculator scenario
- 2:33isinstance approach
- 7:27Object-oriented programming approach
- 15:15Dynamic dispatch with functools.singledispatch
- 26:03Catamorphism
- 37:53Choosing technique
- 38:57Conclusion








