Skip to content

Instantly share code, notes, and snippets.

@jasonlvhit
Created March 1, 2014 09:44
Show Gist options
  • Save jasonlvhit/9287636 to your computer and use it in GitHub Desktop.
Save jasonlvhit/9287636 to your computer and use it in GitHub Desktop.
#Python 装饰器
#http://programmingbits.pythonblogs.com/27_programmingbits/archive/50_function_decorators.html
def memoize(f):
cache = {}
def helper(x):
if x not in cache:
cache[x] = f(x)
return cache[x]
return helper
@memoize
def fib(n):
if n in (0, 1):
return n
else:
return fib(n - 1) + fib(n - 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment