Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Last active June 21, 2017 16:17
Show Gist options
  • Save dsaiztc/0469f558d967f3196546e8f76dad28a9 to your computer and use it in GitHub Desktop.
Save dsaiztc/0469f558d967f3196546e8f76dad28a9 to your computer and use it in GitHub Desktop.
Pure function result cache decorator.
from functools import wraps
def cache(func):
saved = {}
@wraps(func)
def newfunc(*args):
if args in saved:
return saved[args]
result = func(*args)
saved[args] = result
return result
return newfunc
@cache
def web_lookup(url):
return urllib.urlopen(url).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment