Created
October 30, 2015 02:24
-
-
Save spinningD20/81ff4987f47ad8e41bc5 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
from functools import wraps | |
def memoized(f): | |
"""Decorator that caches a function's return value each time it is | |
called. If called later with the same arguments, the cached value | |
is returned, and not re-evaluated. | |
""" | |
cache = {} | |
@wraps(f) | |
def wrapped(*args): | |
try: | |
result = cache[args] | |
except KeyError: | |
result = cache[args] = f(*args) | |
return result | |
return wrapped |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment