python - 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
我会很感激你的提示,为什么最后一行会导致问题:)
解决方案
好的,我想通了;)
代替
return wrap_func()
应该有
return wrap_func
......
推荐阅读
- angular - 用于 TS/Angular 的键入继电器连接模型
- firebase - How to create multiple maps under the same array
- spring - 带有 Spring-Data-JPA 的 BLOB 类型
- node.js - 在 GET 请求中传递查询字符串,express 将查询字符串中的“+”字符转换为空格。如何避免这种情况?
- corda - Corda:受阻令牌可能存在延迟问题
- node.js - 错误:抱歉,将 Node 项目从 Windows 转移到 Debian 时客户端已经太多了
- php - 在 PHP 中提交简单表单时得到失败的结果
- xamarin - 在 Xamarin 中,在设置 DynamicResource 之前使用 RemoveDynamicResource 的原因是什么?
- c# - C# 串口读取数据速度在 9600 或 115200 波特率下相同
- flutter - 使用 DataTable Flutter 的搜索栏布局