1
# -*- test-case-name: epsilon.test.test_remember -*-
4
This module implements a utility for managing the lifecycle of attributes
5
related to a particular object.
8
from epsilon.structlike import record
10
class remembered(record('creationFunction')):
12
This descriptor decorator is applied to a function to create an attribute
13
which will be created on-demand, but remembered for the lifetime of the
14
instance to which it is attached. Subsequent accesses of the attribute
15
will return the remembered value.
17
@ivar creationFunction: the decorated function, to be called to create the
18
value. This should be a 1-argument callable, that takes only a 'self'
19
parameter, like a method.
24
def __get__(self, oself, type):
26
Retrieve the value if already cached, otherwise, call the
27
C{creationFunction} to create it.
29
remembername = "_remembered_" + self.creationFunction.func_name
30
rememberedval = oself.__dict__.get(remembername, None)
31
if rememberedval is not None:
33
rememberme = self.creationFunction(oself)
34
oself.__dict__[remembername] = rememberme
39
__all__ = ['remembered']