Created
June 14, 2015 12:27
-
-
Save andreagrandi/707c15315d2d57bbcb01 to your computer and use it in GitHub Desktop.
Python memoize decorator: cache values already calculated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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