Author rhettinger
Recipients ncoghlan, rhettinger, serhiy.storchaka, thesheep
Date 2013-12-03.20:50:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1386103813.56.0.760349818017.issue19859@psf.upfronthosting.co.za>
In-reply-to
Content
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()

    ...
History
Date User Action Args
2013-12-03 20:50:13rhettingersetrecipients: + rhettinger, ncoghlan, serhiy.storchaka, thesheep
2013-12-03 20:50:13rhettingersetmessageid: <1386103813.56.0.760349818017.issue19859@psf.upfronthosting.co.za>
2013-12-03 20:50:13rhettingerlinkissue19859 messages
2013-12-03 20:50:13rhettingercreate