Decorators with parameters?

前端 未结 13 1426
我在风中等你
我在风中等你 2020-11-21 22:58

I have a problem with the transfer of variable \'insurance_mode\' by the decorator. I would do it by the following decorator statement:

@execute_complete_rese         


        
13条回答
  •  执念已碎
    2020-11-21 23:23

    The syntax for decorators with arguments is a bit different - the decorator with arguments should return a function that will take a function and return another function. So it should really return a normal decorator. A bit confusing, right? What I mean is:

    def decorator_factory(argument):
        def decorator(function):
            def wrapper(*args, **kwargs):
                funny_stuff()
                something_with_argument(argument)
                result = function(*args, **kwargs)
                more_funny_stuff()
                return result
            return wrapper
        return decorator
    

    Here you can read more on the subject - it's also possible to implement this using callable objects and that is also explained there.

提交回复
热议问题