首页 > 解决方案 > TypeError:“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

我会很感激你的提示,为什么最后一行会导致问题:)

标签: pythontypeerrorpython-decoratorsnonetype

解决方案


好的,我想通了;)

代替

 return wrap_func()

应该有

 return wrap_func

......


推荐阅读