Created
September 30, 2013 12:57
-
-
Save 0atman/6763450 to your computer and use it in GitHub Desktop.
This file contains 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): | |
""" | |
Usage: | |
@memoize | |
def my_func(): | |
... | |
""" | |
cache = obj.cache = {} | |
@functools.wraps(obj) | |
def memoizer(*args, **kwargs): | |
key = str(args) + str(kwargs) | |
if key not in cache: | |
cache[key] = obj(*args, **kwargs) | |
return cache[key] | |
return memoizer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment