The decorator pattern
A second interesting structural pattern to learn about is the decorator pattern, which allows a programmer to add responsibilities to an object dynamically, and in a transparent manner (without affecting other objects).
There is another reason why this pattern is interesting to us, as you will see in a minute.
As Python developers, we can write decorators in a Pythonic way (meaning using the language’s features), thanks to the built-in decorator feature.
Note
A Python decorator is a callable (function, method, or class) that gets a func_in
function object as input and returns another function object, func_out
. It is a commonly used technique for extending the behavior of a function, method, or class.
For more details on Python’s decorator feature, see the official documentation: https://docs.python.org/3/reference/compound_stmts.html#function
But this feature should not be completely new to you. We have already encountered commonly...