Creating Decorators That Will Always Work
Learn how to use descriptors to ensure our decorators always work.
There are several different scenarios in which decorators might apply. It can also be the case that we need to use the same decorator for objects that fall into these different multiple scenarios, for instance, if we want to reuse our decorator and apply it to a function, a class, a method, or a static method.
If we create the decorator, just thinking about supporting only the first type of object we want to decorate, we might notice that the same decorator does not work equally well on a different type of object. A typical example is where we create a decorator to be used on a function, and then we want to apply it to a method of a class, only to realize that it does not work. A similar scenario might occur if we design our decorator for a method, and then we want it to also apply for static methods or class methods.
When designing decorators, we typically think about reusing code, so we will want to use that decorator for functions and methods as well. ...