Util.py 338 B

12345678910111213
  1. def memoized(fn):
  2. """Decorator: memoize a function."""
  3. memory = {}
  4. def memoized_fn(*args, **kwargs):
  5. key = (args, tuple(sorted(kwargs.items())))
  6. try:
  7. result = memory[key]
  8. except KeyError:
  9. result = memory[key] = fn(*args, **kwargs)
  10. return result
  11. return memoized_fn