TypeError:“ NoneType”对象不可调用/装饰器

时间:2020-05-05 07:59:01

标签: python typeerror python-decorators nonetype

代码如下,与课程相应:

def new_decorator(func):

    def wrap_func():
        print("code here before executing func")
        func()
        print("func() has been executed")

    return wrap_func()

@new_decorator
def func_needs_decorator():
    print("this function is in need for a decorator")

func_needs_decorator()

其结果如下:

code here before executing func
this function is in need for a decorator
func() has been executed
Traceback (most recent call last):
  File "decorators.py", line 17, in <module>
    func_needs_decorator()
TypeError: 'NoneType' object is not callable

但是,如果我从代码中删除最后一行(第17行,func_needs_decorator()),则不会出现错误消息,结果如下:

code here before executing func
this function is in need for a decorator
func() has been executed

非常感谢您提示最后一行导致问题的提示:)

1 个答案:

答案 0 :(得分:1)

好,我知道了;)

代替

 return wrap_func()

应该是

 return wrap_func

.....