Skip to content

Instantly share code, notes, and snippets.

Created Nov 11, 2011
What would you like to do?
Python memoisation decorator
from functools import wraps
def memoise(wrapped):
cache = {}
def wrapper(*args, **kwargs):
key = (args, tuple(sorted(kwargs.items())))
if key not in cache:
cache[key] = wrapped(*args, **kwargs)
return cache[key]
return wrapper
def test(*args, **kargs):
return 'fubar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment