Going All Meta (Part 2) – Some Python-Fu

In a previous post (a long, long time ago), I said I was going to talk about metaclasses (or at least show an abuse of them) in Python. I am going to get to that, but I want to set the stage by talking about another topic that isn’t nearly asĀ black-magic-y: decorators. When I’m teaching or training, people commonly ask about decorators because they have seen them and they are confused by them – mainly because a common type of decorator is a function that takes in, modifies, and returns a different function. Huh. Back to meta-ville.

Simply put, a decorator is a Python function with some special characteristics. That Python function takes a single, lonely input. The decorator can either be a function-decorator or a class-decorator. In the first case, the decorator take a function as its input and produces a (modified) function as its output. In the second case, it takes a class as its input and produces a (modified) class as its output.

The raw notebook: Decorator Fun – Function Timing (raw)

As seen by nbviewer: Decorator Fun – Function Timing (through nbviewer)