Created
February 24, 2016 20:36
-
-
Save embayer/91ba82647521443123fb to your computer and use it in GitHub Desktop.
caching 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(f): | |
cache = {} | |
def memoized_function(*args): | |
if args not in cache: | |
cache[args] = f(*args) | |
return cache[args] | |
memoized_function.cache = cache | |
return memoized_function | |
@memoize | |
def foo(a, b): | |
return a + b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment