advanced programming techniques class decorators |just as we can create decorators for functions and methodswe can also create decorators for entire classes class decorators take class object (the result of the class statement)and should return class--normally modified version of the class they decorate in this subsection we will study two class decorators to see how they can be implemented sortedlist in we created the sortedlist custom collection class that aggregated plain list as the private attribute self __list eight of the sortedlist methods simply passed on their work to the private attribute for examplehere are how the sortedlist clear(and sortedlist pop(methods were implementeddef clear(self)self __list [def pop(selfindex=- )return self __list pop(indexthere is nothing we can do about the clear(method since there is no corresponding method for the list typebut for pop()and the other six methods that sortedlist delegateswe can simply call the list class' corresponding method this can be done by using the @delegate class decorator from the book' util module here is the start of new version of the sortedlist class@util delegate("__list"("pop""__delitem__""__getitem__""__iter__""__reversed__""__len__""__str__")class sortedlistthe first argument is the name of the attribute to delegate toand the second argument is sequence of one or more methods that we want the delegate(decorator to implement for us so that we don' have to do the work ourselves the sortedlist class in the sortedlistdelegate py file uses this approach and therefore does not have any code for the methods listedeven though it fully supports them here is the class decorator that implements the methodsdef delegate(attribute_namemethod_names)def decorator(cls)nonlocal attribute_name if attribute_name startswith("__")attribute_name "_cls __name__ attribute_name for name in method_namessetattr(clsnameeval("lambda self* **kw"self { { }(* **kw)formatattribute_namename))return cls return decorator