I'm studying the decorator and the wrapper by experimenting examples and I found the behavior of decorator that I can't understand.
def decorator_function(original_function):
def wrapper_function():
print "wrapper is executed before {}".format(original_function.__name__)
return original_function() # Try it without ()
return wrapper_function
@decorator_function
def display():
print("display function ran!")
when I run above function:
In [59]: display()
wrapper is executed before display
display function ran!
However, when I move the print function outside of the wrapper, it seems the wrapper doesn't run at all. (Probably it ran but I don't see the evidence.)
def decorator_function(original_function):
def wrapper_function():
#print "wrapper is executed before {}".format(original_function.__name__)
return original_function() # Try it without ()
print "wrapper is executed before {}".format(original_function.__name__)
return wrapper_function
@decorator_function
def display():
print("display function ran!")
Then I don't see "wrapper is executed before..." anymore. How come? What am I missing here?
In [63]: display()
display function ran!