As a closing note for this chapter, let's review some examples of good decorators and how they are used both in Python itself, as well as in popular libraries. The idea is to get guidelines on how good decorators are created.
Before jumping into examples, let's first identify traits that good decorators should have:
- Encapsulation, or separation of concerns: A good decorator should effectively separate different responsibilities between what it does and what it is decorating. It cannot be a leaky abstraction, meaning that a client of the decorator should only invoke it in black box mode, without knowing how it is actually implementing its logic.
- Orthogonality: What the decorator does should be independent, and as decoupled as possible from the object it is decorating.
- Reusability: It is desirable that the decorator can be applied to multiple...