October 2018
Beginner to intermediate
466 pages
12h 2m
English
The decorator pattern is useful in Python, but there are other options. For example, we may be able to use monkey-patching (for example, socket.socket.send = log_send) to get a similar effect. Single inheritance, where the optional calculations are done in one large method, could be an option, and multiple inheritance should not be written off just because it's not suitable for the specific example seen previously.
In Python, it is very common to use this pattern on functions. As we saw in a previous chapter, functions are objects too. In fact, function decoration is so common that Python provides a special syntax to make it easy to apply such decorators to functions.
For example, we can look at the logging example in ...