Created
July 10, 2014 18:44
-
-
Save justmytwospence/625f6b87534fa7eca563 to your computer and use it in GitHub Desktop.
A decorator that "caches" results of an arbitrary function based on its arguments.
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 cache(func): | |
saved = {} | |
@wraps(func) | |
def newfunc(*args): | |
if args in saved: | |
return newfunc(*args) | |
result = func(*args) | |
saved[args] = result | |
return result | |
return newfunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use: