Author rhettinger
Recipients Dennis Sweeney, docs@python, eric.snow, maximeLeurent, rhettinger, serhiy.storchaka
Date 2021-08-01.14:57:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627829847.4.0.475214005773.issue44782@roundup.psfhosted.org>
In-reply-to
Content
Possible replacement recipes:

-- Implement as a class -----------------

class LRU:

    def __init__(self, func, maxsize=128):
        self.func = func
        self.d = OrderedDict()

    def __call__(self, *args):
        if args in self.d:
            value = self.d[args]
            self.d.move_to_end(args)
            return value
        value = self.func(*args)
        if len(self.d) >= self.maxsize:
            self.d.popitem(False)
        self.d[args] = value
        return value

-- Implement as a closure ---------------

def lru_cache(maxsize):
    def deco(func):
        d = OrderedDict()
        def inner(*args):
            if args in d:
                d.move_to_end(args)
                return d[args]
            answer = func(args)
            d[args] = answer
            if len(d) > maxsize:
                d.popitem(False)
            return answer
        return inner
    return deco
History
Date User Action Args
2021-08-01 14:57:27rhettingersetrecipients: + rhettinger, docs@python, eric.snow, serhiy.storchaka, Dennis Sweeney, maximeLeurent
2021-08-01 14:57:27rhettingersetmessageid: <1627829847.4.0.475214005773.issue44782@roundup.psfhosted.org>
2021-08-01 14:57:27rhettingerlinkissue44782 messages
2021-08-01 14:57:27rhettingercreate