Decorators with parameters?

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

    Writing a decorator that works with and without parameter is a challenge because Python expects completely different behavior in these two cases! Many answers have tried to work around this and below is an improvement of answer by @norok2. Specifically, this variation eliminates the use of locals().

    Following the same example as given by @norok2:

    import functools
    
    def multiplying(f_py=None, factor=1):
        assert callable(f_py) or f_py is None
        def _decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                return factor * func(*args, **kwargs)
            return wrapper
        return _decorator(f_py) if callable(f_py) else _decorator
    
    
    @multiplying
    def summing(x): return sum(x)
    
    print(summing(range(10)))
    # 45
    
    
    @multiplying()
    def summing(x): return sum(x)
    
    print(summing(range(10)))
    # 45
    
    
    @multiplying(factor=10)
    def summing(x): return sum(x)
    
    print(summing(range(10)))
    # 450
    

    Play with this code.

    The catch is that the user must supply key,value pairs of parameters instead of positional parameters and the first parameter is reserved.

提交回复
热议问题