1

我是 python 新手,在网上找不到答案。假设我有一个方法可以进行一些计算并返回一个字符串

def output(self)
     a=self.some_long computation1()
     b=self.some_long computation2()
     c=self.some_attribute
     some_computatuion_inside
     return output_string

我在几个地方使用这个函数并想记住它,但由于它不带参数并且取决于可以在调用之间更改的实例属性,我不知道如何继续,

我意识到我可以编写自己的记忆功能来检查这些属性是否发生了变化,但这似乎不正确,因为这仅适用于该功能,我希望将来可能对其他功能做同样的事情

4

2 回答 2

1

装饰器可以根据任何参数计算密钥。任何实例方法都有一个参数,即self,并且使用它来获取方法的 memoize 装饰器非常容易:

CACHE = {}

def memoize(*attributes):
    def decorator(meth):
        def wrapper(self):
            key = tuple(getattr(self, attr) for attr in attributes)
            try:
                result = CACHE[key]
            except KeyError:
                result = CACHE[key] = meth(self)
            return result
        return wrapper
    return decorator

用法:

class Factorial(object):
    def __init__(self, n):
        self.n = n
    @memoize('n')
    def compute(self):
        if self.n in (0,1): return self.n
        else:
            return Factorial(self.n-1).compute() + Factorial(self.n-2).compute()


f = Factorial(100)

f.compute()
Out[4]: 354224848179261915075L
于 2013-06-18T11:15:26.063 回答
1

一种方法是提取变量(假设它们是可散列的)并使其成为静态方法

@staticmethod
def output_static(a, b, c)
     some_computatuion_inside
     return output_string

然后从类中调用它:

self.output_static(a=self.some_long_computation1()
                   b=self.some_long_computation2()
                   c=self.some_attribute)

你也可以使用 techinque 来记忆some_long_computation1some_long_computation1

如果你愿意,你可以编写一个中间辅助函数,它会简单地调用(memomized)静态方法:

def output(self):
    return self.output_static(a=self.some_long_computation1()
                              b=self.some_long_computation2()
                              c=self.some_attribute)
于 2013-06-18T10:56:30.230 回答