类的缓存属性

问题描述 投票:0回答:2

我喜欢

cached_property
模块:https://pypi.python.org/pypi/cached-property

有没有办法获取类的缓存属性?

这是“类属性”的示例

class Foo(object):
    @classmethod
    def get_bar(cls):
        return 'bar'

    @cached_class_property
    def bar(cls):
        return 'bar'

用途:

assert Foo.bar == Foo.get_bar()

“缓存”是指对属性的第二次(及以后)调用返回第一次调用的值。

python properties
2个回答
1
投票

属性只不过是描述符,当我们定义描述符时,它总是在对象的类型上查找。例如,描述符将在其类上查找,类似地,对于类,它将在其类型(即元类)上查找。

import random


class cached_class_property(object):
    def __init__(self, func):
        self.func = func
        self.name = '_' + func.__name__

    def __get__(self, obj, type):
        if obj is None:
            return self
        sentinel = object()
        value = getattr(obj, self.name, sentinel)
        if value is sentinel:
            value = self.func(obj)
            setattr(obj, self.name, value)
        return value


class Meta(type):

    @cached_class_property
    def bar(cls):
        value = random.random()
        return value


class Foo(object):
    __metaclass__ = Meta

演示:

>>> %run so.py
>>> Foo.bar
0.3896508798298206
>>> Foo.bar
0.3896508798298206
>>> Foo.bar
0.3896508798298206
>>> class Bar(Foo):
...     pass
...
>>> Bar.bar
0.3896508798298206
>>> Bar.bar
0.3896508798298206

0
投票

你可以使用这个模式

import functools

class DataSet:

    def __init__(self, sequence_of_numbers):
        self._data = tuple(sequence_of_numbers)

    @classmethod
    @property # equivalent to "lambda f: property(fget=f)"
    @functools.cache
    def thing5(cls):
        print("executing thing5")
        return 25

print(DataSet.thing5)
print(DataSet.thing5)

产生这个输出

executing thing5
25
25
© www.soinside.com 2019 - 2024. All rights reserved.