I've read all the answers to both these two questions: Why do we need wrapper function in decorators? and Why are decorator functions designed in the way they are?, but I still don't get it.
Why is a decorator written as such:
def decorator_function(function):
def wrapper_function():
print('Before the decoration')
function()
print('After the decoration')
return wrapper_function
Rather than just like this:
def decorator_function(function):
print('Before the decoration')
function()
print('After the decoration')
return function
And why is is that when I do this:
@decorator_function
def original_function():
print('Original function')
In the first case, with the wrapper function, if I call the function like this original_function()
the return is:
Before the decoration
Original function
After the decoration
But in the second case, without the wrapper function, if I call the function, this is the result instead:
Before the decoration
Original function
After the decoration
Original function
The original function gets called one more time
Why do I need the wrapper function?