类的Python functools.wraps等效项

类的Python functools.wraps等效项,第1张

类的Python functools.wraps等效项

每个人似乎都错过显而易见解决方案

>>> import functools>>> class memoized(object):    """Decorator that caches a function's return value each time it is called.    If called later with the same arguments, the cached value is returned, and    not re-evaluated.    """    def __init__(self, func):        self.func = func        self.cache = {}        functools.update_wrapper(self, func)  ## TA-DA! ##    def __call__(self, *args):        pass  # Not needed for this demo.>>> @memoizeddef fibonacci(n):    """fibonacci docstring"""    pass  # Not needed for this demo.>>> fibonacci<__main__.memoized object at 0x0156DE30>>>> fibonacci.__name__'fibonacci'>>> fibonacci.__doc__'fibonacci docstring'


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5653740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存