您的位置:首页 > 编程语言 > Python开发

python 基础——实现一个带缓存功能的函数

2016-08-26 16:20 519 查看
from functools import wraps

def cache(func):
data = {}
@wraps(func)
def wrapper(*args):
if args in data:
print "in cache"
return data[args]
else:
print "not in cache"
res = func(*args)
data[args] = res
return res
return wrapper

@cache
def post_data(args):
return args

post_data(123)    # not in cache
post_data(123)    # in cache
post_data(1235)    # not in cache
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐