Skip to content

Instantly share code, notes, and snippets.

@robcowie
Created Nov 11, 2011
Embed
What would you like to do?
Python memoisation decorator
from functools import wraps
def memoise(wrapped):
cache = {}
@wraps(wrapped)
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
"""
@memoise
def test(*args, **kargs):
return 'fubar'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment