Skip to content

Instantly share code, notes, and snippets.

@robcowie
Created November 11, 2011 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcowie/1357800 to your computer and use it in GitHub Desktop.
Save robcowie/1357800 to your computer and use it in GitHub Desktop.
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