On every computer I’ve had for the past 20 years, I’ve created a folder called “stupid python tricks”. It’s where I put code that should never see the light of day. Code that abuses features like decorators, metaclasses, and dynamic typing to do terrible things.
Code I’m going to teach you.
I have a GitHub repository that contains a compilation of all the awful code I’ve written over the years, abusing features like metaclasses, decorators, various dunder methods, and the mutable nature of Python’s underlying data structures.
It’s a funny way to see how Python works under the hood, but it’s also educational.
Each of the tricks is entertainingly terrible. But they’re also mostly self-contained ways to see what happens when you override certain behaviours of the Python language. Anyone with an intermediate level of Python understanding should be able to follow along and learn something.
I will cover:
- Replacing an imported module with a class, so attribute lookups can be @properties, with hilarious results.
- Overriding the
__sub__magic method to create a hyphenated, fuzzy-matching version of a boolean.
__getattr__to save time implementing all the functions in the
__eq__to make things that aren’t really the same look the same.
This talk is an entertaining tour of the Python Data Model, and the things you can do - if you’re so inclined.