Decorators with parameters?

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

    In my instance, I decided to solve this via a one-line lambda to create a new decorator function:

    def finished_message(function, message="Finished!"):
    
        def wrapper(*args, **kwargs):
            output = function(*args,**kwargs)
            print(message)
            return output
    
        return wrapper
    
    @finished_message
    def func():
        pass
    
    my_finished_message = lambda f: finished_message(f, "All Done!")
    
    @my_finished_message
    def my_func():
        pass
    
    if __name__ == '__main__':
        func()
        my_func()
    

    When executed, this prints:

    Finished!
    All Done!
    

    Perhaps not as extensible as other solutions, but worked for me.

提交回复
热议问题