I am trying to write a decorator for an instance method, as follows:
from functools import wraps
def plus_decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
return 1 + f(*args, **kwargs)
return wrapper
@plus_decorator
def return_i(i):
return i
class A(object):
@plus_decorator
def return_i(self, i):
return i
@plus_decorator
@classmethod
def return_i_class(cls, i):
return i
@plus_decorator
@staticmethod
def return_i_static(i):
return i
if __name__ == '__main__':
print return_i(1)
a = A()
print a.return_i(1)
print A.return_i_class(1)
print A.return_i_static(1)
However, it pops up the error:
AttributeError: 'classmethod' object has no attribute '__module__'
I am wondering why the decorator does not work on classmethod and staticmethod. I think the decorator mostly just passes all the parameters it receives to the wrapper, and only modifies the result. How can I modify the decorator to make it work for classmethod and staticmethod?
A()to call it, e.g.a = A(); a.return_i(1). Perhaps you also need@classmethod?