Decorators

Standard

One of my great weaknesses is that I know next to nothing about object oriented design patterns (as in the Gang of 4 book). By “next to nothing”, I meant that I understand singleton and can even see when it’s needed or useful, but then again, everyone knows at least that pattern.

Today, my research student Chris Main called my attention to a piece of Python code using the Fabric library, which mentioned decorators. In a moment of “holy ignorance, Batman!”, I thought that although I didn’t know what a decorator was, I knew it was a design pattern. And I felt good that, at the very least, I knew what the concept was. And how awfully mistaken I was.

A decorator design pattern is one which means to change the behavior of an object at run time, by extending its functionality, without changing the other instances of that class. In Python programming, decorators are not quite the same thing: they are a type of syntactic modifier that is meant to change a method or a function. Wikipedia seems to have a good explanation of the two concepts: