
Le Thu, 30 Apr 2009 12:22:48 -0700, Scott David Daniels <Scott.Daniels@Acm.Org> s'exprima ainsi:
This is slightly better (name change as in Antoine Pitrou's comment):
class cached(object):
def __init__(self, function): self._function = function self._cache = {}
def __call__(self, *args): try: return self._cache[args] except KeyError: self._cache[args] = self._function(*args) return self._cache[args]
def expire(self, *args): del self._cache[args]
(Aside from the hashable issue pointed by Arnaud) I wonder about having the whole parameter tuple as key for caching. In packrat parsing, you may have more than one parameter (including the source, indeed) but only one is relevant for memoizing (the position). Cache has to be reset anyway when starting a new parse, so that having the source in keys is irrelevant. Typically, if not a single value, I guess saved results form a simple array depending on an ordinal. Denis ------ la vita e estrany