使用堆叠python装饰器的函数参数发生了什么变化?
我不知道如何将原始参数传递给堆栈装饰器。以下是我的例子:使用堆叠python装饰器的函数参数发生了什么变化?,python,arguments,decorator,wrapper,python-3.7,Python,Arguments,Decorator,Wrapper,Python 3.7,我不知道如何将原始参数传递给堆栈装饰器。以下是我的例子: def outside_decorator(function): @functools.wraps(function) def wrapper(*args, **kwargs): print('outside args:',inspect.getfullargspec(function).args) return function(*args,**kwargs) return wra
def outside_decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
print('outside args:',inspect.getfullargspec(function).args)
return function(*args,**kwargs)
return wrapper
def inside_decorator(function):
@functools.wraps(function)
def innerwrapper(*args,**kwargs):
print('inside args:', inspect.getfullargspec(function).args)
return function(*args,**kwargs)
return innerwrapper
class trial:
@outside_decorator
@inside_decorator
def __init__(self,val1,val2,val3,note1='hello',note2='hi'):
print('decorated function args:',inspect.getfullargspec(trial).args)
print('arg values:',val1, val2, val3, note1, note2)
a=trial(1,2,3)
输出为:
outside args: []
inside args: ['self', 'val1', 'val2', 'val3', 'note1', 'note2']
decorated function args: []
arg values: 1 2 3 hello hi
我看到一些答案引用了@functools.wrapps()
中的一个错误,并使用@decorator.decorator
包含decorator模块来修饰decorator,但它使decorator或wrapper只接受allow位置参数输入。我试过每一种组合,但我不知道是正面还是反面
如何通过堆叠的装饰器保留参数?下面是一个在Python中如何使用装饰器中的参数的好例子:
请记住,在Python中,一切都是一个对象,当您试图用Python中的另一个函数来装饰一个函数时,这意味着您只是将内部函数用作外部函数的参数。我不确定您的意思。你的装饰师不接受任何论点。如果您摆脱了
检查…
,只需打印args
和kwargs
,您就可以看到您已经可以访问args了。您想做什么?您是否意识到装饰器只是将函数作为参数,因此您的试用。
方法实际上是外部装饰器(内部装饰器(试用。
初始装饰器(*args,**kwargs))。