This question is related to another question I just asked.
I have created a python decorator as shown below. I want this decorator to accept an unknown list of arguments. But there is an added wrinkle. The decorator is an instance method of another class:
#!/usr/bin/env python
from functools import wraps
class A:
def my_decorator(self, func=None, **kwargs):
def inner_function(decorated_function):
def wrapped_func(*fargs, **fkwargs):
print kwargs
return decorated_function(*fargs, **fkwargs)
return wrapped_func
if func:
return inner_function(func)
else:
return inner_function
class B:
my_a = A()
@my_a.my_decorator(arg_1="Yolo", arg2="Bolo")
def my_func():
print "Woo Hoo!"
my_B = B()
B.my_func()
But this code below doesn't work:
Traceback (most recent call last):
File "./decorator_test.py", line 30, in <module>
B.my_func()
TypeError: unbound method wrapped_func() must be called with B instance as first argument (got nothing instead)
How to fix it?