[英]How can i print function default arguments in decorator function?
This my code :这是我的代码:
def test_deco(func):
def innrer_func(*args,**kwargs):
print('args : ',args)
print('kwargs : ',kwargs)
return func(*args , **kwargs)
return innrer_func
@test_deco
def add (x = 2, y= 3 ) :
return x + y
print(add())
And this is my output :这是我的输出:
args : ()
kwargs : {}
5
And my question is why *args
and **kwargs
is empty but i have right result and how can i pass x
and y
into the test_deco
?我的问题是为什么*args
和**kwargs
是空的,但我有正确的结果,我如何将x
和y
传递给test_deco
?
I must use inspect
module to do this.我必须使用inspect
模块来做到这一点。
import inspect
def test_deco(func):
def innrer_func(*args):
result = func(*args)
try:
print(f'{args[0]} + {args[1]} = {result}')
except IndexError:
default_args = inspect.getcallargs(func , *args)
print(f'{default_args["x"]} + {default_args["y"]} = {result} | Default Values')
finally:
return result
return innrer_func
@test_deco
def add (x = 5, y = 8 ) :
return x + y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.