[issue19859] functools.lru_cache keeps objects alive forever
Raymond Hettinger
report at bugs.python.org
Tue Dec 3 21:50:13 CET 2013
Raymond Hettinger added the comment:
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()
...
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19859>
_______________________________________
More information about the Python-bugs-list
mailing list