Last active
April 21, 2016 18:46
-
-
Save winogradoff/cb47c5443570da850f1934e830ddb5fd to your computer and use it in GitHub Desktop.
Python memoize decorator
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(obj): | |
cache = obj.cache = {} | |
@functools.wraps(obj) | |
def memoizer(*args, **kw): | |
if args not in cache: | |
cache[args] = obj(*args, **kw) | |
return cache[args] | |
return memoizer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment