Created
March 1, 2014 09:44
-
-
Save jasonlvhit/9287636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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