I'm making a decorator that prints out the input and output values of a function when it is called. I wonder how I can get the parameter name of an argument passing into the function. In the sample function below, I expect that the first argument - 5, will be of the parameter 'multiplier' and the rest will be of '*numbers', but how can I transfer those information into the decorator, because the f function in the decorator only have **args and **kwargs as parameter, not the actual parameter name of the function? Hope you guys can help me, thanks a lot.
Here is the decorator:
def log(with_input=True):
def inner(f):
def wrapper(*args, **kwargs):
rv = f(*args, **kwargs)
print('*' * 40)
print("function: ", f.__name__,inspect.signature(f))
if with_input:
print("input: ")
for arg in args:
print(' param_name',arg)
print("output:", rv)
return rv
return wrapper
return inner
This is the sample code to test the decorator:
@log(with_input=True)
def multiplier_sum(multiplier,*numbers):
sum = 0
for n in numbers:
sum += n
return sum * multiplier
multiplier_sum(5,2,3,4)
This is the output, where the parameter name associated with the argument is what I'm expected to in place of param_name:
****************************************
function: multiplier_sum (multiplier, *numbers)
input:
param_name 5
param_name 2
param_name 3
param_name 4
output: 45