Introduction
Python is rich with powerful features and expressive syntax. One of my favorites is decorators. In the context of design patterns, decorators dynamically alter the functionality of a function, method or class without having to directly use subclasses. This is ideal when you need to extend the functionality of functions that you don't want to modify. We can implement the decorator pattern anywhere, but Python facilitates the implementation by providing much more expressive features and syntax for that.
In this post I will be discussing Python's function decorators in depth, accompanied by a bunch of examples on the way to clear up the concepts. All examples are in Python 2.7 but the same concepts should apply to Python 3 with some change in the syntax. Essentially, decorators work as wrappers, modifying the behavior of the code before and after a target function execution, without the need to modify the function itself, augmenting the original functionality, thus decorating it.
What you need to know about functions
Before diving in, there are some prerequisites that should be clear. In Python, functions are first class citizens, they are objects and that means we can do a lot of useful stuff with them.
- Assign functions to variables
- def greet(name):
- return "hello "+name
- greet_someone = greet
- print greet_someone("John")
- # Outputs: hello John
- def greet(name):
- def get_message():
- return "Hello "
- result = get_message()+name
- return result
- print greet("John")
- # Outputs: Hello John
- def greet(name):
- return "Hello " + name
- def call_func(func):
- other_name = "John"
- return func(other_name)
- print call_func(greet)
- # Outputs: Hello John
In other words, functions generating other functions.
- def compose_greet_func():
- def get_message():
- return "Hello there!"
- return get_message
- greet = compose_greet_func()
- print greet()
- # Outputs: Hello there!
More commonly known as a closure. A very powerful pattern that we will come across while building decorators. Another thing to note, Python only allows read access to the outer scope and not assignment. Notice how we modified the example above to read a "name" argument from the enclosing scope of the inner function and return the new function.
- def compose_greet_func(name):
- def get_message():
- return "Hello there "+name+"!"
- return get_message
- greet = compose_greet_func("John")
- print greet()
- # Outputs: Hello there John!
Function decorators are simply wrappers to existing functions. Putting the ideas mentioned above together, we can build a decorator. Let's consider a function that wraps the string output of another function by p tags.
- def get_text(name):
- return "lorem ipsum, {0} dolor sit amet".format(name)
- def p_decorate(func):
- def func_wrapper(name):
- return "{0}
".format(func(name)) - return func_wrapper
- my_get_text = p_decorate(get_text)
- print my_get_text("John")
- # Outputs lorem ipsum, John dolor sit amet
- get_text = p_decorate(get_text)
- print get_text("John")
- # Outputs lorem ipsum, John dolor sit amet
Python's Decorator Syntax
Python makes creating and using decorators a bit cleaner and nicer for the programmer through some syntactic sugar To decorate get_text we don't have to get_text = p_decorator(get_text) There is a neat shortcut for that, which is to mention the name of the decorating function before the function to be decorated. The name of the decorator should be perpended with an @ symbol.
- def p_decorate(func):
- def func_wrapper(name):
- return "{0}
".format(func(name)) - return func_wrapper
- @p_decorate
- def get_text(name):
- return "lorem ipsum, {0} dolor sit amet".format(name)
- print get_text("John")
- # Outputs lorem ipsum, John dolor sit amet
- def p_decorate(func):
- def func_wrapper(name):
- return "{0}
".format(func(name)) - return func_wrapper
- def strong_decorate(func):
- def func_wrapper(name):
- return "{0}".format(func(name))
- return func_wrapper
- def div_decorate(func):
- def func_wrapper(name):
- return "{0}".format(func(name))
- return func_wrapper
- get_text = div_decorate(p_decorate(strong_decorate(get_text)))
- @div_decorate
- @p_decorate
- @strong_decorate
- def get_text(name):
- return "lorem ipsum, {0} dolor sit amet".format(name)
- print get_text("John")
- # Outputs lorem ipsum, John dolor sit amet
In Python, methods are functions that expect their first parameter to be a reference to the current object. We can build decorators for methods the same way, while taking self into consideration in the wrapper function.
- def p_decorate(func):
- def func_wrapper(self):
- return "{0}
".format(func(self)) - return func_wrapper
- class Person(object):
- def __init__(self):
- self.name = "John"
- self.family = "Doe"
- @p_decorate
- def get_fullname(self):
- return self.name+" "+self.family
- my_person = Person()
- print my_person.get_fullname()
- def p_decorate(func):
- def func_wrapper(*args, **kwargs):
- return "{0}
".format(func(*args, **kwargs)) - return func_wrapper
- class Person(object):
- def __init__(self):
- self.name = "John"
- self.family = "Doe"
- @p_decorate
- def get_fullname(self):
- return self.name+" "+self.family
- my_person = Person()
- print my_person.get_fullname()
Looking back at the example before the one above, you can notice how redundant the decorators in the example are. 3 decorators(div_decorate, p_decorate, strong_decorate) each with the same functionality but wrapping the string with different tags. We can definitely do much better than that. Why not have a more general implementation for one that takes the tag to wrap with as a string? Yes please!
- def tags(tag_name):
- def tags_decorator(func):
- def func_wrapper(name):
- return "<{0}>{1}</{0}>".format(tag_name, func(name))
- return func_wrapper
- return tags_decorator
- @tags("p")
- def get_text(name):
- return "Hello "+name
- print get_text("John")
- # Outputs Hello John
Debugging decorated functions
At the end of the day decorators are just wrapping our functions, in case of debugging that can be problematic since the wrapper function does not carry the name, module and docstring of the original function. Based on the example above if we do:
- print get_text.__name__
- # Outputs func_wrapper
Functools to the rescue
Fortunately Python (as of version 2.5) includes the functools module which contains functools.wraps. Wraps is a decorator for updating the attributes of the wrapping function(func_wrapper) to those of the original function(get_text). This is as simple as decorating func_wrapper by @wraps(func). Here is the updated example:
- from functools import wraps
- def tags(tag_name):
- def tags_decorator(func):
- @wraps(func)
- def func_wrapper(name):
- return "<{0}>{1}</{0}>".format(tag_name, func(name))
- return func_wrapper
- return tags_decorator
- @tags("p")
- def get_text(name):
- """returns some text"""
- return "Hello "+name
- print get_text.__name__ # get_text
- print get_text.__doc__ # returns some text
- print get_text.__module__ # __main__
Where to use decorators
The examples in this post are pretty simple relative to how much you can do with decorators. They can give so much power and elegance to your program. In general, decorators are ideal for extending the behavior of functions that we don't want to modify. For a great list of useful decorators I suggest you check out the Python Decorator Library.
More reading resources
- What is a decorator?
- Decorators I: Introduction to Python Decorators
- Python Decorators II: Decorator Arguments
- Python Decorators III: A Decorator-Based Build System
- Guide to: Learning Python Decorators by Matt Harrison
沒有留言:
張貼留言