Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created June 14, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreagrandi/707c15315d2d57bbcb01 to your computer and use it in GitHub Desktop.
Save andreagrandi/707c15315d2d57bbcb01 to your computer and use it in GitHub Desktop.
Python memoize decorator: cache values already calculated
def memoize(function):
cache = {}
def decorated_function(*args):
if args in cache:
return cache[args]
else:
val = function(*args)
cache[args] = val
return val
return decorated_function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment