Decorator将赢得工作:' nontype'对象不可呼应
import numpy as np
def multi_print(n):
def decorator(func):
def wrapper(*args, **kwargs):
for z in np.arange(n):
print(func(*args))
@multi_print(2)
def if_num(x):
if x == 1:
return 'abc'
else:
return 'def'
if_num(3)
我正在尝试使用装饰者如何与功能进行互动。当我运行此代码块时,我会得到一个'nontype'对象是不可callable
错误。不确定为什么。我希望上述代码将两次打印“ DEF”。
import numpy as np
def multi_print(n):
def decorator(func):
def wrapper(*args, **kwargs):
for z in np.arange(n):
print(func(*args))
@multi_print(2)
def if_num(x):
if x == 1:
return 'abc'
else:
return 'def'
if_num(3)
I am trying to just play around with how decorators interact with functions. When I run this block of code, I get a 'NoneType' object is not callable
error. Wasn't sure why. I would expect the above code to print "def" two times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的装饰师和包装纸什么都不会返回。带有争论的装饰符应返回装饰器,并且装饰器应返回包装功能。修复:
输出:
Your decorator and wrapper don't return anything. A decorator with arguments should return a decorator, and the decorator should return the wrapped function. The fix:
Output: