创建仅在第一次调用时计算的属性。
memoize的Python项目详细描述
只在第一次调用属性时计算它们的值。最简单的解释方法是举例说明。
错误:
class Foo(object): def __init__(self): self._bar_cache = None @property def bar(self): if self._bar_cache is None: self._bar_cache = 2 * 2 * 2 return self._bar_cache
好:
from memoize import mproperty class Foo(object): @mproperty def bar(self): return 2 * 2 * 2