# 装饰器函数
def foo(fn):
def wrappers():
print("hello, %s" %fn.__name__)
fn()
print("bye, %s" %fn.__name__)
return wrappers
@foo
def fooo():
print("i am fooo")
fooo()
from functools import wraps
import time
def demof(fn):
cache={}
miss=object()
@wraps(fn)
def www(*args):
result=cache.get(args,miss)
if result is miss:
result=fn(*args)
cache[args]=result
return result
return www
@demof
def fib(n):
if n<2:
return n
return fib(n-1)+fib(n-2)
st=time.time()
for i in range(3,30):
fib(i)
se=time.time()
print(se-st)
def fib1(n):
if n<2:
return n
return fib1(n-1)+fib1(n-2)
st=time.time()
for i in range(3,30):
fib1(i)
se=time.time()
print(se-st)
def dumpstep(fn):
cache={}
miss=object()
@wraps(fn)
def wwww(*args):
result=cache.get(args,miss)
if result is miss:
result=fn(*args)
cache[args]=result
return result
return wwww
@dumpstep
def Step(n):
if n<2:
return 1
if n<3:
return 2
return Step(n-1)+Step(n-2)
for i in range(1,100):
print(Step(i),end=" , ")
if i%10==0:
print("\n")