1

首先是“智能功能”的概念:

>>> 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
>>> 

这意味着,如果你给出相同的参数,它只计算一次。当没有第一次调用时,它直接返回值。

我对这种功能评价很高。因为有了它,你不必定义一个变量来存储值,还可以节省计算源。

但是这个函数太简单了,当我想定义另一个智能函数g时,我必须重复一些类似的东西:

>>> def g(x,check={}):
    if x not in check:
        print 'call g'
        check[x]=x**2
    return check[x]

所以,我的问题出现了,如何定义一个装饰器“懒惰”,它的工作方式如下:

@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 时,ie:func(1,2) 也需要智能工作。提前谢谢!

4

2 回答 2

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)
于 2013-10-20T17:29:29.933 回答
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)
于 2013-10-20T17:50:50.503 回答