Werkzeug 有一个cached_property
装饰器,可以完全满足您的需求。它只是将__dict__
第一次函数调用后的函数条目替换为第一次调用的输出。
这是代码(来自github 上的 werkzeug.utils并稍微编辑了长度):
_missing = object()
class cached_property(object):
"""A decorator that converts a function into a lazy property. The
function wrapped is called the first time to retrieve the result
and then that calculated result is used the next time you access
the value::
class Foo(object):
@cached_property
def foo(self):
# calculate something important here
return 42
The class has to have a `__dict__` in order for this property to
work.
"""
# implementation detail: this property is implemented as non-data
# descriptor. non-data descriptors are only invoked if there is
# no entry with the same name in the instance's __dict__.
# this allows us to completely get rid of the access function call
# overhead. If one choses to invoke __get__ by hand the property
# will still work as expected because the lookup logic is replicated
# in __get__ for manual invocation.
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
def __get__(self, obj, type=None):
if obj is None:
return self
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
(顺便说一句 - 发布代码或链接到这样的代码更好吗?)
如果您想了解更多关于它为什么起作用的信息,请查看描述符上的 Python 文档。@property
上面的代码创建了一个允许覆盖它的非数据描述符(与 不同)。