How to change a function's return using a decorator?

后端 未结 2 1768
野趣味
野趣味 2020-12-16 12:52

I want create a decorator to change a function\'s return value like that, How to do that like below?:

def dec(func):
    def wrapper():
        #some code...         


        
2条回答
  •  粉色の甜心
    2020-12-16 13:11

    Well.... you call the decorated function and change the return value:

    def dec(func):
        def wrapper(*args, **kwargs):
            result = func(*args, **kwargs)
            result['c'] = 3
            return result
        return wrapper
    

提交回复
热议问题