All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Exploring Python's __subclasshook__ and Pattern Matching Edge Cases

By

agluszak

9mo ago· 4 min readenInsight

Summary

The article explores creative and potentially problematic uses of Python's __subclasshook__ feature, particularly how it can be combined with Python 3.10's pattern matching to create non-standard type behaviors. The author demonstrates how to make objects match patterns they shouldn't technically match, essentially "tricking" Python's pattern matching system. This is presented as a fun but potentially dangerous exploration of Python's type system capabilities.

Key quotes

· 4 pulled
Abstract Base Classes with __subclasshook__ can define what counts as a subclass of the ABC, even if the target doesn't know about the ABC
You can do some weird stuff with this
Then Python 3.10 added pattern matching
Let's make the CPython team regret adding pattern matching to Python!
Snippet from the RSS feed
Let's make the CPython team regret adding pattern matching to Python!

You might also wanna read