当前位置:网站首页>装饰器解决TypeError: ‘NoneType‘ object is not callable
装饰器解决TypeError: ‘NoneType‘ object is not callable
2022-07-19 11:59:00 【Eden_mm】
参考:https://blog.csdn.net/qq_41320433/article/details/104347119
今天跑代码发现报错:
TypeError: ‘NoneType’ object is not callable
翻译:“NoneType” 对象不可调用
分析解决:
callable(可调用)对象是指一个后面可以加 ‘( ) ’的对象
既然报错是 ‘不可调用’,那就去掉调用函数的 ‘( )’ 即可。
代码示例:
看代码 #后面的注释就懂了
# 装饰器 <== 高阶函数 + 嵌套函数
def decorator(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
timer = end_time - start_time
print("run time spend :", timer, 's', sep='')
return wrapper() # 这里的'()'会导致报错'NoneType' object is not callable
# 只要去掉wrapper后面的括号即可解决问题
@decorator # 引用装饰器,等同于 run_func = decorator(run_func)
def run_func():
a = int(input("input x:").strip(' '))
b = int(input("input y:").strip(' '))
print("%d**%d = %d" % (a, b, a**b))
run_func() # 装饰器不改变被装饰函数的源代码和调用方式
# 有人说去掉run_func后面的‘()’也可以解决问题,但我觉得不可取,原因看下面
网上有人说去掉被装饰函数(原函数)后面的 ‘( )’ ,虽然也可以让程序不报错,我却觉得这违背了装饰器的意义:装饰器在不改变被装饰函数的源代码和调用方式的情况下增加新的功能,如果去掉原函数的括号那不就是改变调用方式了吗,所以不可取。
# 使用装饰器
def zhuang_shi_qi(func):
def out():
func()
print(func.__name__)
return out # 这里的'()'会导致报错'NoneType' object is not callable
# 只要去掉out后面的括号即可解决问题
def whatever():
pass
return whatever
@zhuang_shi_qi
def func1():
pass
@zhuang_shi_qi
def func2():
pass
if __name__ == '__main__':
func1()
func2()
边栏推荐
- Redis支撑秒杀场景的关键技术和实践都有哪些?
- Ask a question: scenario: the cumulative window is used in Flink SQL. The window size is one day, and the cumulative of the current day is counted every minute
- 省选专练之 [HAOI2009]毛毛虫
- 自定义持久层框架MyORMFrameworkJDBC回顾和问题分析,自定义持久层框架思路分析
- Preparation of SILVACO diode, triode and CMOS
- 在线XML转JSON工具
- 科技云报道:零信任和SASE有什么不一样?答案其实并不重要
- Devops has been practiced for many years. What is the most painful thing?
- Comparison of eolink and JMeter interface tests
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
猜你喜欢
mos管的输入输出特性曲线及gm/id仿真曲线(cadence IC617)
Cloud Native Core Technology: Service Mesh Implementation - istio
【历史上的今天】6 月 30 日:冯·诺依曼发表第一份草案;九十年代末的半导体大战;CBS 收购 CNET
康威定律——组织决定产品,领域驱动设计
Jsd-2204-weibo project (completion) -day16
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
CB Insights发布AI行业七大趋势:合成数据、多模态AI崛起
[cloud native] event publishing and subscription in Nacos -- observer mode
About XML file (VIII) -dom and validation
Week 5 Image Classification、Bag of Visual Words (Bag of Features) and Multi-Layer Neural Networks
随机推荐
在线XML转JSON工具
Week 5 Image Classification、Bag of Visual Words (Bag of Features) and Multi-Layer Neural Networks
UICollectionViewCell和UITableViewCell上下左右留空白
【BZOJ2393】Cirno的完美算数教室
EXCRT/扩展中国剩余定理学习笔记
【历史上的今天】7 月 19 日:IMAP 协议之父出生;Project Kotlin 公开亮相;CT 成像实现新突破
剑指offer题库总结(一)之数组(C语言版本)
C#/VB. Net to add multi line text watermark to word document
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
【历史上的今天】7 月 2 日:BitTorrent 问世;商业系统 Linspire 被收购;索尼部署 PlayStation Now
科技云报道:不止于零信任,派拉“数字安全云战略”的野望
实例|APICloud AVM框架封装滑动单元格组件
自定义持久层框架MyORMFramework(一)—JDBC分析和解决思路
Was expecting double quote to start field name error
Quelles sont les principales techniques et pratiques de redis pour soutenir les scènes de second kill?
Oracle的$sqlarea 是不是无法查出来这些SQL是那台主机执行的?
云原生核心技术之:Service Mesh 的实现—— Istio
【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
ArrayList of set