Dark Magic in Python 3.10's Pattern Matching: Exploiting `__subclasshook__`
2025-08-22
This article explores the unexpected capabilities arising from the combination of Python 3.10's pattern matching and the `__subclasshook__` method of Abstract Base Classes (ABCs). By cleverly using `__subclasshook__`, the author demonstrates 'hijacking' pattern matching, allowing custom definition of which types match and even matching based on object attributes, not just types. While showcasing powerful functionalities like creating custom matchers, the author strongly cautions against using this technique in production code due to its unpredictable and potentially harmful nature.
Development
Abstract Base Classes