Manually calling a decorator that takes arguments
decorator, python
Solution
You need something like:
def f1():
return True
f1 = validate_arguments(arg1, arg2)(f1)
Here `validate_arguments(arg1, arg2)` returns the actual decorator, to that decorator we pass the function object `f1`, which in turn returns the new modified function.
Demo:
def validate_arguments(arg1, arg2):
def decorator(func):
def wrapped():
print arg1, arg2
return func()
return wrapped
return decorator
def f1():
return True
f1 = validate_arguments(1, 2)(f1)
print f1()
#1 2
#True
Problem
I have a function f1: ``` def f1(): return True ``` I also have a decorator that takes arguments, that can be used like this: ``` @validate_arguments(arg1, arg2) ``` I am trying to call `f1` manually without the `@` (for testing and reuse purposes) but that doesn't seem to work. So something like: ``` validate_arguments(f1, arg1, arg2) ``` The reason it does not work is because `validate_arguments` is a function that takes the arguments as parameters, and contains a closure that is the actual decorator. Is there no way to do what I want? To manually call the decorator on a function without the `@`, for a decorator that takes arguments?