python decorator模块_Python decorator module

使用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 incache:

cache[key]= func(*args, **kw)returncache[key]defmemoize(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={}returndecorate(f, _memoize)>>>@memoize

...defheavy_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 insorted(kw))print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))return f(*args, **kw)deftrace(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 insorted(kw))

...print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))

...return f(*args, **kw)

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值