Skip to content

Instantly share code, notes, and snippets.

@Katharine
Created April 4, 2012 20:29
Show Gist options
  • Save Katharine/2305344 to your computer and use it in GitHub Desktop.
Save Katharine/2305344 to your computer and use it in GitHub Desktop.
def cached(f):
def wrapper(*args):
if args not in wrapper.cache:
wrapper.cache[args] = f(*args)
return wrapper.cache[args]
wrapper.cache = {}
return wrapper
def factorial(n):
print "Running factorial"
result = 1
for i in range(2, n + 1):
result = result * i
return result
factorial = cached(factorial)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment