#coding=utf-8
import time
class CacheDecorator():
__cache={}
def __init__(self,func):
self.func=func
def __call__(self, *args, **kwargs):
if self.func.__name__ in CacheDecorator.__cache:
return CacheDecorator.__cache[self.func.__name__]
else:
value=self.func(*args,**kwargs)
CacheDecorator.__cache[self.func.__name__]=value
return value
def cost_time(func):
def inner(*args,**kwargs):
start=time.time()
result=func(*args,**kwargs)
end=time.time()
print("耗时:","end-start")
return result
return inner
@cost_time
@CacheDecorator
def func1():
print("start func1")
time.sleep(3)
print("end func1")
if __name__=="__main__":
r1=func1()
r2=func1()
print(r1)
print(r2)
老师,我照着文档敲了,但是真的看了好几遍视频都没看懂到底讲的啥意思
首先那个缓存到底是啥,最后执行也完全没体现出干了什么呀,还有这个为什么要搞一个字典在那里呀,我fun1那个函数都没有写return什么为什么它最后还会打印None,
CacheDecorator.__cache
和
CacheDecorator.__cache[self.func.__name__]
到底表示啥
value=self.func(*args,**kwargs)
CacheDecorator.__cache[self.func.__name__]=value
都是value,为什么后面那个value要在后面
return result
为什么加一个这句话