Chapter 12. Class Decorators
Just as we can create decorators for functions and methods, we can also create decorators for entire classes. Class decorators take a class object (the result of the class
statement), and should return a class—normally a modified version of the class they decorate. In this subsection we will study two class decorators to see how they can be implemented.
In the book’s examples there is a SortedList.py
module that defines the Sort-edList
custom collection class. This class aggregates a plain list as the private attribute self.__list
, and eight of the SortedList
class’s methods simply pass on their work to the private attribute. For example, here are how the Sort-edList.clear()
and SortedList.pop()
methods ...
Get Advanced Python 3 Programming Techniques now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.