【发布时间】:2019-10-09 11:31:42
【问题描述】:
我有两个看起来几乎相同的代码 sn-ps。但是,当我运行它们时,第一个代码 sn-p 可以完美运行。当我运行第二个脚本时,出现错误:TypeError: 'NoneType' object is not callable
为什么???
我尝试重命名函数。我尝试更改函数中的文本。
def cough_dec(func):
def func_wrapper():
print("*cough*")
func()
print("*cough*")
return func_wrapper
@cough_dec
def question():
print('can you give me a discount on that?')
@cough_dec
def answer():
print("it's only 50p, you cheapskate")
question()
answer()
=============================================
def c_dec(func):
def wrapper_func():
print('something')
func()
print('anything')
return wrapper_func()
@c_dec
def say_hello():
print("Hello World!")
@c_dec
def bye():
print("Goodbye World!")
say_hello()
bye()
如果我删除第二个 sn-p 中的括号,一切正常,但为什么呢?
【问题讨论】:
-
在第一个sn-p中,你的装饰器cough_dec返回一个函数
return func_wrapper,在第二个sn-p中你的装饰器c_dec返回函数return func_wrapper()的输出,它是None
标签: python python-3.x