[omaha] Decorators
Eli Criffield
elicriffield at gmail.com
Mon Jan 14 23:05:34 CET 2008
I was looking around the web for examples of decorators and i saw the
baypiggies meeting video that goes over decorators for about 30
seconds. They use the "decorator" module. Turns out its in the ubuntu
packages so a simple apt-get install python-decorator installs it.
Using this module now i get what decorators are for!
You can add functionality to any or every method you define without
having to change that method, just add the decorator.
Here's my simple example.
--snip--
from decorator import decorator
@decorator
def trace(f, *args, **kw):
print "call %s with args, %s, %s"%(f.func_name,args,kw)
return f(*args, **kw)
@trace
def sum_three(a, b, c):
return a + b + c
print sum_three(1,2,3)
--snip--
You could just as easy make a logging decorator, or a profiler
decorator to find out where the slowdown is, all kinds of useful
examples pop into my head.
My question is how do you do this without using the decorator module?
What am i missing about decorators, they don't seem that useful
without the decorator module, witch isn't part of core python.
Eli Criffield
More information about the Omaha
mailing list