Im struggling to understand decorator with python. My understanding is here below;
At first, my_function_too(x,y)
method will be defined as func in my_decorator(func)
and then function_that_runs_func(*args,**kwargs)
methods will be outputed.
1. What is the func in @functools.wraps(func)
?
2. Why do we need to write return function_that_runs_func
and return my_decorator
here ?
My friends explained that function_that_runs_func()
method is going to replace my_function_too()
method. But I could not understand what he is saying and why.
3. Would anyone please tell gently me what he is implying ?
CODE:
def decorator_with_arguments(number):
def my_decorator(func):
@functools.wraps(func)
def function_that_runs_func(*args,**kwargs):
print("In the decorator")
if number == 56:
print("Not running the function")
return None
else:
print("Running the 'real' func")
return func(*args,**kwargs)
return function_that_runs_func
return my_decorator
@decorator_with_arguments(57)
def my_function_too(x,y)
print(x+y)
my_function_too(57,67)
OUTPUT:
In the decorator
124