I have created a python decorator as shown below. I want this decorator to accept an unknown list of arguments. But the code below doesn't work.
#!/usr/bin/env python
from functools import wraps
def my_decorator(decorated_function, **kwargs):
print "kwargs = {}".format(kwargs)
@wraps(decorated_function)
def inner_function(*args, **kwargs):
print "Hello world"
return decorated_function(*args, **kwargs)
return inner_function
@my_decorator(arg_1="Yolo", arg2="Bolo")
def my_func():
print "Woo Hoo!"
my_func()
When I run it, I get this error:
File "./decorator_test.py", line 14, in <module>
@my_decorator(arg_1="Yolo", arg2="Bolo")
TypeError: my_decorator() takes exactly 1 argument (0 given)
Why is the **kwargs
not accepting the multiple arguments I'm sending into the decorator? How can I fix it?