Decorator error: NoneType object is not callable

自古美人都是妖i 提交于 2019-12-30 11:55:11

问题


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.


回答1:


You should return the wrapper function itself, not its result:

def tsfunc(func):
    def wrappedFunc():
        print '%s() called' % func.__name__
        return func()
    return wrappedFunc   # Do not call the function, return a reference instead

Decorators replace the decorated item with the return value of the decorator:

@tsfunc
def foo():
    # ....

is equivalent to:

def foo():
    # ....
foo = tsfunc(foo)

which expands to (in your code):

foo = wrappedFunc()

so you were replacing the function foo with the result of the wrappedFunc() call, not with wrappedFunc itself.




回答2:


You need to remove the parentheses in

return wrappedFunc

The decorator is supposed to return the wrapper function, not call it.

With this fix, the code produces:

foo() called
foo() called


来源:https://stackoverflow.com/questions/14536242/decorator-error-nonetype-object-is-not-callable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!