what is the difference between functools.wraps and update_wrapper

前端 未结 2 835
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 01:31

I am not able to find what is the difference between these two python functions.

functools.wraps and update_wrapper

Can some give me

2条回答
  •  轮回少年
    2021-02-04 02:21

    functools.wraps is equivalent to:

    def wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES):
        def decorator(wrapper):
            return update_wrapper(wrapper, wrapped=wrapped, ...)
        return decorator
    

    It's actually implemented using partial instead of an inner function, but the effect is the same.

    The purpose is to allow using it as a decorator:

     @wraps(f)
     def g():
         ...
    

    is equivalent to:

    def g():
        ...
    g = update_wrapper(g, f)
    

提交回复
热议问题