这个装饰器怎么总是报错啊?
def bb(func):def wrapper():
print('正在运行装饰器')
func()
print('装饰器调用结束')
return wrapper
@bb
def aa():
i = 2
if i <= 10:
m = 10/ i
print(m)
else:
print(i)
if __name__ == "__main__":
aa()
Traceback (most recent call last):
File "D:\pythonProject\tipin\test6.py", line 22, in <module>
aa()
TypeError: 'NoneType' object is not callable
把第六行代码 return wrapper 缩进提前一位,不然返回的是 None 导致报错
参考代码:
def bb(func):
def wrapper():
print('正在运行装饰器')
func()
print('装饰器调用结束')
return wrapper
@bb
def aa():
i = 2
if i <= 10:
m = 10/ i
print(m)
else:
print(i)
if __name__ == "__main__":
aa()
页:
[1]