I\'m trying to write a class decorator that applies a decorator to all the class\' methods:
import inspect
def decorate_func(func):
def wrapper(*args,
(Too long for a comment)
I took the liberty of adding the ability to specify which methods should get decorated to your solution:
def class_decorator(*method_names):
def wrapper(cls):
for name, meth in inspect.getmembers(cls):
if name in method_names or len(method_names) == 0:
if inspect.ismethod(meth):
if inspect.isclass(meth.im_self):
# meth is a classmethod
setattr(cls, name, VerifyTokenMethod(meth))
else:
# meth is a regular method
setattr(cls, name, VerifyTokenMethod(meth))
elif inspect.isfunction(meth):
# meth is a staticmethod
setattr(cls, name, VerifyTokenMethod(meth))
return cls
return wrapper
Usage:
@class_decorator('some_method')
class Foo(object):
def some_method(self):
print 'I am decorated'
def another_method(self):
print 'I am NOT decorated'