首先出现了“智能功能”的概念:
>>> def f(x,check={}):
if x not in check:
print 'call f'
check[x]=2*x
return check[x]
>>> f(2)
call f
4
>>> f(2)
4
>>> f(3)
call f
6
>>>
这意味着,如果你给出相同的参数,它只会计算一次。当没有先调用时,它会直接返回值。
我非常重视这种功能。因为有了它,你不必定义一个varible来存储值,而是保存计算源。
但是这个功能太简单了,当我想定义另一个智能功能g时,我必须重复一些:
>>> def g(x,check={}):
if x not in check:
print 'call g'
check[x]=x**2
return check[x]
所以,我的问题出现了,如何定义一个装饰工具“lazy”,其工作方式如下:
@lazy
def func(a,b,*nkw,**kw):
print 'call func'
print a,b
for k in nkw:
print k
for (k,v) in kw.items():
print k,v
#do something with a,b,*kw,**nkw to get the result
result=a+b+sum(nkw)+sum(kw.values())
return result
print '--------1st call--------'
print func(1,2,3,4,5,x=6,y=7)
print '--------2nd call--------'
print func(1,2,3,4,5,x=6,y=7)
结果:
>>>
--------1st call--------
call func
1 2
3
4
5
y 7
x 6
28
--------2nd call--------
28
请注意,当没有* kw或** nkw,即:func(1,2)也需要提前工作smart.thanks!
答案 0 :(得分:3)
来自https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize:
class memoized(object):
'''Decorator. Caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned
(not reevaluated).
'''
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if not isinstance(args, collections.Hashable):
# uncacheable. a list, for instance.
# better to not cache than blow up.
return self.func(*args)
if args in self.cache:
return self.cache[args]
else:
value = self.func(*args)
self.cache[args] = value
return value
def __repr__(self):
'''Return the function's docstring.'''
return self.func.__doc__
def __get__(self, obj, objtype):
'''Support instance methods.'''
return functools.partial(self.__call__, obj)
答案 1 :(得分:1)
在nooodl的帮助下,我从第https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize页找到了我想要的答案。
在此分享:
import collections,functools
def memoize(obj):
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
@memoize
def func(a,b,*nkw,**kw):
print 'call func'
print a,b
for k in nkw:
print k
for (k,v) in kw.items():
print k,v
#do something with a,b,*kw,**nkw to get the result
result=a+b+sum(nkw)+sum(kw.values())
return result
print '--------1st call--------'
print func(1,2,3,4,5,x=6,y=7)
print '--------2nd call--------'
print func(1,2,3,4,5,x=6,y=7)