views:

48

answers:

2
def log(func):
    def wraper(*a, **kw):   
        return func(*a, **kw)
    return wraper

@log
def f():
    print 'f'


print locals()['f'] # - prints <function wraper at 0x00CBF3F0>.

How do you get the real f object (not decorator wrap)?

+3  A: 

You don't.1 Store it if you need to access it later.

def log(func):
  def wrapper(*a, **kw):
    return func(*a, **kw)
  wrapper.func = func
  return wrapper

@log
def f():
  print 'f'

print f.func

1 You could mess with the closure, but I can't recommend it.

Roger Pate
+3  A: 

The functools module also provides a wraps decorator which makes sure that the wrapped function looks more like the real function: correct name, module, and docstring, for example.

Ned Batchelder
+1, learned something new today ;-)
ChristopheD
+1 This should probably be the accepted answer as `functools.wraps` does more than just setting `wrapper.func = func`.
jeffjose
Actually, wraps doesn't seem to do precisely what the OP wanted: it doesn't preserve the original function somewhere. But it may be that the OP didn't want the function, just needed an attribute off of it.
Ned Batchelder