Raymond Hettinger <[email protected]> added the comment:
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
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44782>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com