Decorators with parameters?

前端 未结 13 1407
我在风中等你
我在风中等你 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:28

    In case both the function and the decorator have to take arguments you can follow the below approach.

    For example there is a decorator named decorator1 which takes an argument

    @decorator1(5)
    def func1(arg1, arg2):
        print (arg1, arg2)
    
    func1(1, 2)
    

    Now if the decorator1 argument has to be dynamic, or passed while calling the function,

    def func1(arg1, arg2):
        print (arg1, arg2)
    
    
    a = 1
    b = 2
    seconds = 10
    
    decorator1(seconds)(func1)(a, b)
    

    In the above code

    • seconds is the argument for decorator1
    • a, b are the arguments of func1

提交回复
热议问题