I came across the following code (this is a simplification of the real code) which combines decorators factory and lambda usage:
from functools import wraps
def decorator_factory(arg1):
def decorator(arg2):
return lambda func: real_decorator(arg1, arg2, func)
return decorator
def real_decorator(prefix: str, perm: str, func):
@wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result
return wrapper
my_decorator_instance = decorator_factory("r")
class MyClass():
def __init__(self, *args, **kwargs):
pass
@my_decorator_instance('decorator args')
def method_1(self):
print("method_1")
m = MyClass()
m.method_1()
The code works great but I don't really understand the mechanics of the same. Especially how is the lambda value used when returned as a decorator. Please notice that in the decorator_factory there's no func argument. the func is the arg of lambda.