conditional computation
robert
no-spam at no-spam-no-spam.com
Fri Oct 27 06:26:03 EDT 2006
Bruno Desthuilliers wrote:
> robert a écrit :
> (snip)
>> class MemoCache(dict): # cache expensive Objects during a session
>> (memory only)
>> def memo(self, k, f):
>> try: return self[k]
>> except KeyError: #<--------- was error
>> return self.setdefault(k, f())
>> cache=MemoCache()
>> ...
>>
>> o = cache.memo( complex-key-expr, lambda: expensive-calc-expr )
>>
>
> And how do you get back the cached value without rewriting both
> complex-key-expr *and* expensive-calc-expr ? Or did I missed the point ?
the complex-key-expr is written only once in the code - execution inevitable.
expensive-calc-expr is written only once in code and expensive execution (I have mostly matrix math) is only done at "f()" time.
-robert
More information about the Python-list
mailing list