代码如下,与课程相应。
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
我将感谢你的提示,为什么最后一行会导致这个问题:)
好吧,我想明白了;)
而不是
return wrap_func()
应该有
return wrap_func
.....