summaryrefslogtreecommitdiff
path: root/templates/memoize.py
blob: 95057adaf3464abf1baa577f6ce4a4e9b2b3ad04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
class memoized(object):
   def __init__(self, func):
      self.func = func
      self.cache = {}
   def __call__(self, *args):
      try:
         return self.cache[args]
      except KeyError:
         value = self.func(*args)
         self.cache[args] = value
         return value
      except TypeError:
         return self.func(*args)