I wrote a small piece of code to run it myself and understand the flow:
def perform_arithmetic_ops(func):
print("INSIDE PERFORM ARTITHMETIC")
def decorate(*args):
print("I JUST GOT DECORATED")
print(f"SUM -> {args[0]+args[1]}")
func(args[0],args[1])
return decorate
@perform_arithmetic_ops
def print_me(x,y):
print(f"X = {x} ; Y = {y}")
print_me(5,4)
Please correct me if my understanding, which I've summarized in the form points below, is incorrect.
def perform_arithmetic_ops
is the decorator which only takes a callable object, in this case a function as its argument, correct?The inner function
def decorate()
here is what actually performs the desired modification. What I noticed was commentingfunc(args[0],args[1])
insidedef decorate()
basically meant that theX={x} ; Y={y}
statement wasn't being printed. So does this mean that the the decorator replaces everything the original function does? If yes, how is that a modification? Isn't it more of a replacement for another function?return decorate()
i.e. callingdecorate()
instead of returning it led to errors for tuple index out of range. Why?