2 回答
TA贡献1820条经验 获得超2个赞
装饰器接受一个函数对象(这里是:)other(multiply)
并返回另一个wrap_func()
替换它的函数。该名称other
现在指代替换的函数。
虽然原始函数接受一个参数,但替换函数没有。以所示方式调用带参数的无参数函数失败。
TA贡献1775条经验 获得超8个赞
您传递的函数与使用它的方式之间存在差异。这是跟踪和解决方案。我仔细检查了装饰器看到的函数,然后添加了所需的参数。如果您需要这是通用的,则需要一个通用参数列表,例如*args.
### Set up decorator func here
def decorator_prac(old_func):
#def decorator_prac(old_func):
print("decorator arg", old_func) # Track what is passed in
def wrap_func(func_arg): # Accommodate the function profile
multiply(1,2)
old_func(func_arg) # Implement the proper profile
print(1+7)
return wrap_func
输出:
passed in
2
decorator arg <function other at 0x7f0e7b21b378>
what should I say
2
8
添加回答
举报