是否有一个装饰器来简单地缓存函数返回值? [英] Is there a decorator to simply cache function return values?

查看:21
本文介绍了是否有一个装饰器来简单地缓存函数返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下事项:

@property定义名称(自己):如果不是 hasattr(self, '_name'):# 昂贵的计算self._name = 1 + 1返回 self._name

我是新手,但我认为可以将缓存分解为装饰器.只有我没有找到一个喜欢它;)

PS 真正的计算不依赖于可变值

解决方案

Python 3.8 functools.cached_property 装饰器

https://docs.python.org/dev/library/functools.html#functools.cached_property

来自 Werkzeug 的

cached_property 被提及:https://stackoverflow.com/a/5295190/895245 但一个推测的衍生版本将被合并到 3.8 中,这太棒了.

这个装饰器可以被看作是缓存 @property,或者当你没有任何参数时作为一个更干净的 @functools.lru_cache.

文档说:

<块引用>

@functools.cached_property(func)

将类的方法转换为属性,该属性的值计算一次,然后在实例的生命周期内作为普通属性缓存.与 property() 类似,但添加了缓存.用于实例的昂贵计算属性,否则这些属性实际上是不可变的.

示例:

类数据集:def __init__(self, sequence_of_numbers):self._data = sequence_of_numbers@cached_propertydef stdev(self):返回 statistics.stdev(self._data)@cached_property定义差异(自我):返回 statistics.variance(self._data)

3.8 版中的新功能.

注意此装饰器要求每个实例上的 dict 属性是可变映射.这意味着它不适用于某些类型,例如元类(因为类型实例上的 dict 属性是类命名空间的只读代理),以及那些指定 slots 不包括 dict 作为定义的插槽之一(因为此类类根本不提供 dict 属性).

Consider the following:

@property
def name(self):

    if not hasattr(self, '_name'):

        # expensive calculation
        self._name = 1 + 1

    return self._name

I'm new, but I think the caching could be factored out into a decorator. Only I didn't find one like it ;)

PS the real calculation doesn't depend on mutable values

解决方案

Python 3.8 functools.cached_property decorator

https://docs.python.org/dev/library/functools.html#functools.cached_property

cached_property from Werkzeug was mentioned at: https://stackoverflow.com/a/5295190/895245 but a supposedly derived version will be merged into 3.8, which is awesome.

This decorator can be seen as caching @property, or as a cleaner @functools.lru_cache for when you don't have any arguments.

The docs say:

@functools.cached_property(func)

Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance. Similar to property(), with the addition of caching. Useful for expensive computed properties of instances that are otherwise effectively immutable.

Example:

class DataSet:
    def __init__(self, sequence_of_numbers):
        self._data = sequence_of_numbers

    @cached_property
    def stdev(self):
        return statistics.stdev(self._data)

    @cached_property
    def variance(self):
        return statistics.variance(self._data)

New in version 3.8.

Note This decorator requires that the dict attribute on each instance be a mutable mapping. This means it will not work with some types, such as metaclasses (since the dict attributes on type instances are read-only proxies for the class namespace), and those that specify slots without including dict as one of the defined slots (as such classes don’t provide a dict attribute at all).

这篇关于是否有一个装饰器来简单地缓存函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆