没有所谓的捷径
一切都是时间最平凡的累积

functools模块中的@wraps的实现原理详解

本文最后更新于2019年8月21日,已超过33天没有更新,如果文章内容失效,请反馈给我们,谢谢!

functools模块中的@wraps的实现原理详解

函数wraps,update_wrapper即为@wraps实现过程

import time
from functools import partial
 
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
                       '__annotations__')
WRAPPER_UPDATES = ('__dict__',)


def update_wrapper(wrapper,  # inner
                   wrapped,  # func_test
                   assigned=WRAPPER_ASSIGNMENTS,
                   updated=WRAPPER_UPDATES):
    """Update a wrapper function to look like the wrapped function
 
       wrapper is the function to be updated
       wrapped is the original function
       assigned is a tuple naming the attributes assigned directly
       from the wrapped function to the wrapper function (defaults to
       functools.WRAPPER_ASSIGNMENTS)
       updated is a tuple naming the attributes of the wrapper that
       are updated with the corresponding attribute from the wrapped
       function (defaults to functools.WRAPPER_UPDATES)
    """
    print('update_wrapper 开始执行...')
    for attr in assigned:
        try:
            value = getattr(wrapped, attr)
        except AttributeError:
            pass
        else:
            setattr(wrapper, attr, value)
    for attr in updated:
        getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
    # Issue #17482: set __wrapped__ last so we don't inadvertently copy it
    # from the wrapped function when updating __dict__
    wrapper.__wrapped__ = wrapped
    # Return the wrapper so this can be used as a decorator via partial()
    print('update_wrapper 执行结束')
    return wrapper
 

def wraps(wrapped,
          assigned=WRAPPER_ASSIGNMENTS,
          updated=WRAPPER_UPDATES):
    """Decorator factory to apply update_wrapper() to a wrapper function
 
       Returns a decorator that invokes update_wrapper() with the decorated
       function as the wrapper argument and the arguments to wraps() as the
       remaining arguments. Default arguments are as for update_wrapper().
       This is a convenience function to simplify applying partial() to
       update_wrapper().
    """
    print('wraps 开始执行...')
    print('wraps 执行结束')
    return partial(update_wrapper, wrapped=wrapped,
                   assigned=assigned, updated=updated)
 
 
def record_time(func):
    print('record_time开始执行...')
 
    @wraps(func)  # inner = update_wrapper的返回值
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print("函数运行时间为 %s" % (time.time() - start))
        return result
 
    print('record_time执行结束')  # 当然不是真正的结束,执行完下一行才结束
    return wrapper
 
 
@record_time
def func_test():
    print("func_test 开始执行...")
    time.sleep(1)
    print("func_test 执行结束")
    return
 
 
func_test()
 
"""
打印结果如下:
    record_time 执行...
    wraps 开始执行...
    wraps 执行结束
    update_wrapper 开始执行...
    update_wrapper 执行结束
    record_time 执行结束
    func_test 开始执行...
    func_test 执行结束
    函数运行时间为 1.0000197887420654
 
 
以上就是装饰器执行过程
"""

 

赞(0) 打赏
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除,联系邮箱:lwarm@qq.com。文章观点不代表本站立场。本站原创内容未经允许不得转载,或转载时需注明出处:红岩子 » functools模块中的@wraps的实现原理详解
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

今天所做的努力都是在为明天积蓄力量

联系我们赞助我们