Skip to content

Instantly share code, notes, and snippets.

@0atman
Created September 30, 2013 12:57
Show Gist options
  • Save 0atman/6763450 to your computer and use it in GitHub Desktop.
Save 0atman/6763450 to your computer and use it in GitHub Desktop.
def memoize(obj):
"""
Usage:
@memoize
def my_func():
...
"""
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment