我是一名自学成才的程序员,需要你在 python 中的 @decorator 方面的帮助。
这是我的问题。在我用装饰器运行 other(multiply) 后,它出现了一个错误:wrap_func() 需要 0 个位置参数,但给出了 1 个。我不知道为什么以及如何解决这个问题。我的主要目的是学习装饰器的工作原理;因此以下代码可能没有意义。
def multiply(a,b):
return a*b
###pass in multiply function in other()
def other(multiply):
print('passed in')
print(multiply(1,2))
other(multiply)
### result shows passed in and 2, as expected
### Set up decorator func here
def decorator_prac(old_func):
def wrap_func():
multiply(1,2)
old_func()
print(1+7)
return wrap_func
###add decorator on def other(multiply)
@decorator_prac
def other(multiply):
print('what should I say')
print(multiply(1,2))
###Run other(multiply)
other(multiply)
输出:
passed in
2
Traceback (most recent call last):
File "so.py", line 28, in <module>
other(multiply)
TypeError: wrap_func() takes 0 positional arguments but 1 was given
DIEA
www说
相关分类