Hi Whit!
whit wrote:
yuppie wrote:
I ended up using this method as decorator for most methods:
def memoize(func):
memo = {}
def memoized_func(*args):
if args not in memo:
memo[args] = func(*args)
return memo[args]
return memoized_func
Are there better ways to resolve this?
Will those memo dicts be removed together with the view object or does
this create a potential memory leak? (I'm not very familiar with
decorators.)
that's pretty elegant compared to shoving a multitude of values into the
view. The closest thing to it would be a PEAK binding, but even that
can't handle the variety of situations that keing the memo by function
signature gives you. very nice.
Besides the fact it doesn't work :(
that looks safe; as far as I can tell when the decorated method is
garbage collected with the view, all refs to 'memo' should be reaped..
Digging a bit deeper it looks like decoraters are created on class
level, not on instance level. So 'memo' is not garbage collected with
the view, it's the same for all instances and grows with each request.
That's not exactly the behavior I want.
Have to think about it a bit more. Any input is welcome.
In python 2.4 you could use frozenset to have keyword support too. I
think something like this would work:
> def memoize(func):
> memo = {}
> def memoized_func(*args, **kwargs):
> sig = (args, frozenset(kwargs.items()))
> if sig not in memo:
> memo[sig] = func(*args, **kwargs)
> return memo[sig]
> return memoized_func
Thanks, I'll keep that in mind. For now this is not my problem.
Cheers,
Yuppie
_______________________________________________
Zope-CMF maillist - [email protected]
http://mail.zope.org/mailman/listinfo/zope-cmf
See http://collector.zope.org/CMF for bug reports and feature requests