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)

.

原文地址:https://www.cnblogs.com/autoria/p/6848466.html