I am currently working on a class decorator in Python for my example class. Below is the code containing the original class, the class decorator, and the class containing the function decorators. This code compiles and works as desired. I have omitted some functions from the example class for brevity.
def class_decorator(*method_names):
def class_rebuilder(cls):
class NewClass(cls):
def __getattribute__(self, attr_name):
obj = super(NewClass, self).__getattribute__(attr_name)
if (hasattr(obj, '__call__') and (attr_name in method_names)):
if (attr_name == 'function1'):
return example_ClassDecorator().function1_decorator(obj)
...
return obj
return NewClass
return class_rebuilder
class example_ClassDecorator():
@classmethod
def function1_decorator(cls, example_function1):
def decorator(*args ,**kwargs):
print ('Inside the decorator for function1 with args: ', args)
return example_function1(*args , **kwargs)
return decorator
...
@class_decorator('function1',...)
class example(object):
def __init__(self):
pass
def function1(self, arg1):
print ('I am in example function1 with arg: ', arg1)
if (arg1 == 'test'):
print 'testing'
...
I arrived at this code through research as to how to decorate methods and classes and python. As mentioned before, this code works. My issue is that I don't entirely understand the logic of what is going on.
Here is my current understanding of the code:
- I know that the
@class_decorator('function1','function2',...)
tag above the example class tells python that the given functions for this class are to be decorated. These strings are the*method_names
passed into theclass_decorator
function. class_decorator
then calls itsclass_rebuilder(cls)
function which returns the classNewClass(cls)
which will contain the newly decorator functionsNewClass
then calls__getattribute__(self, attr_name)
.obj
is used to save the (function) object passed in.if (hasattr(obj, '__call__') and (attr_name in method_names)):
confirms thatobj
is a function (by making sure it has the__call__
attribute associated with all function objects) and that it is one of the functions to be decorated.A series of if statements decides which function decorator to be called from the
example_ClassDecorator
function.
The class_decorator
definition is where most of my questions lie.
- How is
class_rebuilder
function called? Does python call it itself? - What populates the
attr_name
parameter in__getattribute__
method? - This line:
obj = super(NewClass,self).__getattribute__(attr_name)
It appears to be saving the named attribute object so as to check that it is a function. But I don't understand what is going on with the super call.
Any help explaining this code or a more prefered method to decorate classes in python would be greatly appreciated.