Python 官方文档:入门教程 => 点击学习
class Memoize(object): def __init__(self, func): self.func = func # self.cache = {(ADMIN_USER, ADMI
class Memoize(object):
def __init__(self, func):
self.func = func
# self.cache = {(ADMIN_USER, ADMIN_PASSWord): ADMIN_USER, }
self.cache = {}
def __call__(self, *args):
print 'args: ', args
print 'cache: ', self.cache
try:
return self.cache[args]
except KeyError:
value = self.func(*args)
self.cache[args] = value
return value
except TypeError:
return self.func(*args)
def __repr__(self):
return self.func.__doc__ or ''
def __get__(self, obj, objtype):
"""
Support instance methods. Important
:param obj:
:param objtype:
:return:
"""
print "obj : ", obj
print "objtype : ", objtype
return functools.partial(self.__call__, obj)
def __str__(self):
return str(self.func)
--结束END--
本文标题: 实现cache缓存(Python)
本文链接: https://www.lsjlt.com/news/191182.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0