memoize.py 430 B

1234567891011121314
  1. # Copyright 2013 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. def memoize(fn):
  5. '''Decorates |fn| to memoize.
  6. '''
  7. memory = {}
  8. def impl(*args, **optargs):
  9. full_args = args + tuple(optargs.items())
  10. if full_args not in memory:
  11. memory[full_args] = fn(*args, **optargs)
  12. return memory[full_args]
  13. return impl