Skip to content

Instantly share code, notes, and snippets.

@spinningD20
Created October 30, 2015 02:24
Show Gist options
  • Save spinningD20/81ff4987f47ad8e41bc5 to your computer and use it in GitHub Desktop.
Save spinningD20/81ff4987f47ad8e41bc5 to your computer and use it in GitHub Desktop.
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