I wrote a function decorator like this:
def tsfunc(func):
def wrappedFunc():
print '%s() called' % func.__name__
return func()
return wrappedFunc()
@tsfunc
def foo():
pass
foo() # to get it work, use foo instead of foo()
foo()
I got following error message:
foo() called
Traceback (most recent call last):
File "decorator.py", line 11, in <module>
foo()
TypeError: 'NoneType' object is not callable
I get it work by replacing “foo()” with “foo”. but I still didn’t get the result I expected:
foo() called
seems like the foo function is only called once.
Please help me understand why this is happening.
You should return the wrapper function itself, not its result:
Decorators replace the decorated item with the return value of the decorator:
is equivalent to:
which expands to (in your code):
so you were replacing the function
foowith the result of thewrappedFunc()call, not withwrappedFuncitself.