类型错误:'NoneType'对象不可调用装饰符。

问题描述 投票:0回答:1

代码如下,与课程相应。

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

我将感谢你的提示,为什么最后一行会导致这个问题:)

python typeerror python-decorators nonetype
1个回答
0
投票

好吧,我想明白了;)

而不是

 return wrap_func()

应该有

 return wrap_func

.....

© www.soinside.com 2019 - 2024. All rights reserved.