To understand it better first imagine that your code is like this:
def decorator(*args, **kwargs):
def inner(func):
# code functionality here
print("Inside inner function")
print("I like", kwargs['like'])
func()
# returning inner function
return inner
@decorator(like="geeksforgeeks")
def my_func():
print("Inside actual function")
When you are using a decorator the decorator function executes and returns a function which contains your original function that has been decorated. The goal of a decorator is to decorate your function. So it gets your function, include it in another function which the decorator wants (in your case a function with two print
statements, and then return the newly created function. So when this function executes it runs the two print statements. As a result, the output of the above code would look like this:
Inside inner function
I like geeksforgeeks
Inside actual function
To conclude, your original function is wrapped in the inner
function. In the inner
function, first the two print
statements get executed and the your original function gets executed. So the output would look like above.
Now, imagine that you have added another print statement at the beginning of the decorator function (just like the code you have provided in your question). Remember I told you that a decorator creates a function and returns it, that is because the decorator is a function itself. So when you include a print statement at the beginning of the decorator, it would print it even before wrapping the original function. So the first statement that gets printed is the print
statement at the beginning of the decorator function. After that, the inner
function is created and eventually, it will be executed.
So the output would look like this:
Inside decorator
Inside inner function
I like geeksforgeeks
Inside actual function
Now see your second code. The second program has an inner
function which just returns a function. No execution of that function is provided. So the inner
function is called and as a result, another function is returned WITHOUT calling it.