这听起来像是你需要一些持久性记忆。这是初级的,但可能让你开始:
import shelve
class MemoizedProcessor(object):
def __init__(self):
# writeback only if it can't be assured that you'll close this shelf.
self.preprocessed = shelve.open('preprocessed.cache', writeback = True)
if 'inputargs' not in self.preprocessed:
self.preprocessed['inputargs'] = dict()
def __del__(self, *args):
self.preprocessed.close()
def process(self, *args):
if args not in self.preprocessed['inputargs']:
self._process(*args)
return self.preprocessed['inputargs'][args]
def _process(self, *args):
# Something that actually does heavy work here.
result = args[0] ** args[0]
self.preprocessed['inputargs'][args] = result
像调试? – fvrghl
或者像pickle这样的持久数据存储来保存中间值? –
我建议传递一个命令行参数来告诉它是否跳过最初的东西。除非你真的真的需要一个通用的解决方案,然后看看泡菜 – ahuff44