如何解决有人可以告诉我为什么这个python装饰器不起作用吗?
def decoratorrt(func):
def innerht():
print("hello there")
func()
print("it has ended")
return innerht
@decoratorrt
def hellot():
print("hello","are you in")
hellot()
我收到的错误消息是TypeError Traceback(最近一次通话最近) 在 ----> 1 hellot()
TypeError:“ NoneType”对象不可调用
解决方法
您缩进了return innerht
;应该是decoratorrt
的返回值,但是如果没有返回值,decoratorrt
会隐式返回None
(而innerht
会试图返回自身)。固定功能:
def decoratorrt(func):
def innerht():
print("hello there")
func()
print("it has ended")
return innerht # Dedented
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。