Python makes metaprogramming possible, but every version of Python has a few nuances (and not completely compatible), which makes the path to metaprogramming more rugged. The use of a class of function objects has a long history, and there are also techniques for exploring and implementing magic properties. In version 2.2, Python added a helpful custom meta-class mechanism, but at the cost of racking up the user's brains. Recently, in version 2.4, Python added "decorator", the latest way to perform most metaprogramming--and the most user-friendly way to date.
Less labor, more.
Decorator has something in common with the metaprogramming abstractions introduced before Python: Even without these technologies, you can implement the functionality they provide. As Michele Simionato and I pointed out in an earlier article in the lovely Python column, even in Python 1.5, the creation of a Python class is possible without the need for a "meta-class" hook.
Decorator is fundamentally mediocre and very similar. The function implemented by decorator is to modify the functions and methods defined immediately after decorator. This is always possible, but this functionality is primarily driven by the Classmethod () and Staticmethod () built-in functions introduced in Python 2.2. In the old style, you can call Classmethod (), as follows:
Listing 1. Typical "old-fashioned" classmethod
class C:
def foo(cls, y):
print "classmethod", cls, y
foo = classmethod(foo)
Although Classmethod () is a built-in function, it is not unique; You can also use your own methods to transform functions. For example:
Listing 2. Conversion of a typical "legacy" method
def enhanced(meth):
def new(self, y):
print "I am enhanced"
return meth(self, y)
return new
class C:
def bar(self, x):
print "some method says:", x
bar = enhanced(bar)
What decorator does is prevent you from reusing the method name, and place the decorator where it is first mentioned in the method definition and its name. For example:
Listing 3. Typical "old-fashioned" classmethod
class C:
@classmethod
def foo(cls, y):
print "classmethod", cls, y
@enhanced
def bar(self, x):
print "some method says:", x