[omaha] Decorators

Jeff Hinrichs JeffH at delasco.com
Tue Jan 15 00:04:30 CET 2008

Not quite sure about the 3rd party decorator module, but with stock 2.4

-----Original Message-----
From: omaha-bounces at python.org [mailto:omaha-bounces at python.org] On
Behalf Of Eli Criffield
Sent: Monday, January 14, 2008 4:58 PM
To: Omaha Python Users Group
Subject: Re: [omaha] Decorators

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


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

#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
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), {}

Eli Criffield
Omaha Python Users Group mailing list
Omaha at python.org

More information about the Omaha mailing list