• Python decorator module


    Python functool

    Python中的装饰器

    Python decorator module document

    使用functool中的wraps,可以复制一些信息(__name__, __doc__, __module__, __dict__),但是signature还是会被改变(比如参数信息),要保留正确的参数信息,可以使用decorate,从接口上看decorate和update_wrapper相反,这和api的命名就有关了:

    update_wrapper wrapper with wrapped.

    decorate function with a caller function (the caller function describing the functionality of the decorator).

    特别之处在于,decorate方法要求参数中的caller function具备完整的signature

    The caller function must have signature (f, *args, **kw), and it must call the original function f with arguments args and kw, implementing the wanted capability (in this case, memoization)

    以下面的例子来说,_memoize是caller function,memoize是decorator

    def _memoize(func, *args, **kw):
        if kw:  # frozenset is used to ensure hashability
            key = args, frozenset(kw.items())
        else:
            key = args
        cache = func.cache  # attribute added by memoize
        if key not in cache:
            cache[key] = func(*args, **kw)
        return cache[key]
    
    def memoize(f):
        """
        A simple memoize implementation. It works by adding a .cache dictionary
        to the decorated function. The cache will grow indefinitely, so it is
        your responsability to clear it, if needed.
        """
        f.cache = {}
        return decorate(f, _memoize)
    
    >>> @memoize
    ... def heavy_computation():
    ...     time.sleep(2)
    ...     return "done"
    
    >>> print(getargspec(heavy_computation))
    ArgSpec(args=[], varargs=None, varkw=None, defaults=None)


    使用decorator模块可以防止更改signature,这样decorator符合一个signature-preserving decorators的要求:

    Callable objects which accept a function as input and return a function as output, with the same signature.


    来看另外一个例子

    def _trace(f, *args, **kw):
        kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
        print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
        return f(*args, **kw)
    def trace(f):
        return decorate(f, _trace)

    先了解一下结构:

    python中单下划线开头代表这是一个内部函数,这里是_trace;

    _trace是decorator内部描述装饰器功能的一个函数,也可以说是wrapper,f是那个wrapped;

    与之前的memoize不同,这里的trace只返回内部函数_trace,不声明其余内容;

    所以可以加语法糖!使用@decorator就达到一样的效果,将一个caller function转换成一个signature-reserving decorator。

    >>> @decorator
    ... def trace(f, *args, **kw):
    ...     kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
    ...     print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
    ...     return f(*args, **kw)

    .

  • 相关阅读:
    免费下载!8套最新出炉的扁平化设计风格图标
    modern.IE – Web 开发必备的 IE 浏览器测试工具
    优秀案例!教您如何设计现代简约风格网页
    Mondrian – 开源的矢量图形 Web 应用程序
    超精美!10款专业的高质量名片设计模板
    英文字体下载大全:35款高品质英文字体
    免费下载!Twitter Bootstrap V3 矢量界面素材
    WOW.js – 在页面滚动时展现动感的元素动画效果
    Cool!12幅由小图片组成的创意图像重组作品
    InstantClick – 快速响应!让你的网站与众不同
  • 原文地址:https://www.cnblogs.com/autoria/p/6848466.html
Copyright © 2020-2023  润新知