Attributes
One of the most common uses of decorators—in fact, the reason they're called decorators—is to add attributes to function objects. These attributes can be used by code in other parts of the program to distinguish decorated functions from each other and from under undecorated functions.
Adding attributes is easy. Inside the decorator, assign an attribute to the function, just like we would for any other object, and then return it. Then, elsewhere in the code, check for the attribute and respond accordingly.
That's all well and good and often quite useful, but we could do a lot more with decorators. For example, we can enclose the function in a wrapper that performs some computation before or after calling the function.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access