A decorate pattern is simply a wrapper that is used to extend the behavior of a function without actually modifying the function.
The next example is from The Code Ship where I’ll explain in my own words. If we define a function that prints out a string given a name as an argument.
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
Now if we wanted to extend the get_text
function to add html <p>
tags then we would create a wrapper that takes a generic function as an argument.
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
print get_text("John") # lorem ipsum, John dolor sit amet
Assigning the decorate to a variable allows you to call the decorate function in a typical way.
my_get_text = p_decorate(get_text) # This is what takes place when you add @p_decorate
print my_get_text("John") # <p>lorem ipsum, John dolor sit amet</p>
Python provides syntactic sugar @
which allows you to decorate a function and extend it’s behavior using the mechanics above.
@p_decorate
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
print get_text("John") # <p>lorem ipsum, John dolor sit amet</p>