Created
July 27, 2014 23:17
-
-
Save BrettBukowski/673ac8e2190a85daa2de 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
import cPickle as pickle | |
def memoize(func): | |
results = {} | |
def wrapper(*args): | |
key = pickle.dumps(args) | |
if key not in results: | |
results[key] = func(*args) | |
return results[key] | |
return wrapper | |
@memoize | |
def some_repeatedly_called_func: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment