[omaha] Decorators

Eli Criffield elicriffield at gmail.com
Mon Jan 14 23:58:28 CET 2008


> Would you mind explaining what's going on here? I'm not sure what you're
> doing. I've heard of decorators but never tried using them.

Sure.

from decorator import decorator #simple import of the 3rd party decorator module


@decorator
#you decorate your new decorator, what this does i don't really know....

# the function you define here will kind of run instead of the funtion
you decorate it with, but you can call the real function in this
function, and thats what this does at the end.

def trace(f, *args, **kw):
    print "call %s with args, %s, %s"%(f.func_name,args,kw)
    return f(*args, **kw)

# you decorate your function with the trace decorator you made
@trace
def sum_three(a, b, c):
    return a + b + c

print sum_three(1,2,3)

The output is like this:

call sum_three with args, (1, 2, 3), {}
6

Eli Criffield


More information about the Omaha mailing list