我有以下带参数的装饰器:
from functools import wraps
def pdecor(p):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
p -= 1
return fn(*args, **wargs)
return wrapper
return decorator
尝试使用装饰器会导致:
>>> @pdecor(1)
... def run(): pass
...
>>> run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in wrapper
UnboundLocalError: local variable 'p' referenced before assignment
>>>
为什么我不能更改p
?