我用python编写了一个静态方法,它需要时间来计算,但我希望它只计算一次,然后返回计算值。 我该怎么办? 以下是示例代码:
class Foo:
@staticmethod
def compute_result():
#some time taking process
Foo.compute_result() # this may take some time to compute but store results
Foo.compute_result() # this method call just return the computed result
我想你要做的就是} (python3)或一些short handwritten code(如果您只关心可哈希类型(也适用于python2))。你知道吗
memoizing
。 有几种方法可以使用decorator,其中一种是使用^{可以为一个方法注释多个装饰器。你知道吗
相关问题 更多 >
编程相关推荐