Message205148
I don't think this is an appropriate use of an LRU cache. There are other ways to "freeze" a method return value (typically by storing the result in an instance).
Here's one way of doing it (taken from the source code for Itty https://pypi.python.org/pypi/itty/0.8.2 ):
class lazyproperty(object):
"""A property whose value is computed only once. """
def __init__(self, function):
self._function = function
def __get__(self, obj, _=None):
if obj is None:
return self
value = self._function(obj)
setattr(obj, self._function.func_name, value)
return value
Here is how it is used:
class Request(object):
"""An object to wrap the environ bits in a friendlier way."""
...
@lazyproperty
def POST(self):
return self.build_complex_dict()
... |
|
Date |
User |
Action |
Args |
2013-12-03 20:50:13 | rhettinger | set | recipients:
+ rhettinger, ncoghlan, serhiy.storchaka, thesheep |
2013-12-03 20:50:13 | rhettinger | set | messageid: <1386103813.56.0.760349818017.issue19859@psf.upfronthosting.co.za> |
2013-12-03 20:50:13 | rhettinger | link | issue19859 messages |
2013-12-03 20:50:13 | rhettinger | create | |
|