I learne d (or tried to learn) decorators recently with python. I've coded this, but I actually don't fully understand how it actually works...
The code:
def logging_decorator(fn):
def wrapper(*args, **kwargs):
print(f"You called {fn.__name__}{args}")
result = fn(args[0], args[1], args[2])
print(f"It returned: {result}")
return wrapper
@logging_decorator
def a_function(a, b, c):
return a * b * c
a_function(1, 2, 3)
My question: I dont understand how in the wrapper the args become the inputs (a,b,c) of the function "a_function". it looks like magic to me I don't see how the inputs of the wrapper became those of the function "a_function".
My research: I've watched 3 videos on the subject but none of them really focuses on that aspect of the problem. I've asked on other forums but still didn't get a satisfactory answer. I guess people just assume it's obvious for them it should be to others as well, but as far as I'm concerned it is not! I've had no problem using decorators in the past and understanding until I've tried to add inputs in the wrapper (trying to get the parameters of the function that we fetch to the decorator).
you're answer will be highly appreciated and I thank you in advance!