conditional computation
Mike Kent
mrmakent at cox.net
Thu Oct 26 16:07:26 EDT 2006
robert wrote:
> I want to use a computation cache scheme like
>
>
> o = CACHECOMPUTE complex-key-expr expensive-calc-expr
>
>
> frequently and elegantly without writing complex-key-expr or expensive-calc-expr twice.
> So its ugly:
>
> _=complex-key-expr; o=cache.get(_) or cache.setdefault(_,expensive-calc-expr)
>
> Any ideas?
>
> -robert
Your question is a bit terse, so my answer might not be spot on for it,
but it sounds like you want what is typically called 'memoization',
whereby a function caches its expensive-to-calculate return values,
where the cache is keyed by the function arguments.
See:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52201
and various other implimentations in the Cookbook for details.
More information about the Python-list
mailing list