這聽起來像是你需要一些持久性記憶。這是初級的,但可能讓你開始:
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