If you’ve been in Python-land for long, you’ve probably seen some
@-sign thingies hovering (often mysteriously) above functions and class definitions, saying things like
@classmethod or perhaps something even more obscure. Maybe you already know that these are called “decorators”. Maybe you’ve even used them, or written your own!
Even if you’ve done all that and still don’t quiiiite get what’s going on under the hood with decorators… don’t worry, my friend, you are not alone. Heck, I’m still not quite sure what goes on under the hood with decorators, but after a very productive afternoon of fiddling, I have a much better idea, and I’m here to share the fruits of that fiddling with you. Ready? Here we go:
Decorators are callables called on callables that return a callable which then replaces the original callable.
…Yeah, okay, that’s fair. Let me try that again.