Kenny Tilton:
> This macro:
>
> (defmacro c? (&body code)
> `(let ((cache :unbound))
> (lambda (self)
> (declare (ignorable self))
> (if (eq cache :unbound)
> (setf cache (progn ,@code))
> cache))))
I have about no idea of what that means. Could you explain
without using syntax? My guess is that it caches function calls,
based only on the variable names. Why is a macro needed
for that?
>>> import time
>>> def CachedCall(f):
... cache = {}
... def cached_call(self, *args):
... if args in cache:
... return cache[args]
... x = f(self, *args)
... cache[args] = x
... return x
... return cached_call
...
>>> class LongWait:
... def compute(self, i):
... time.sleep(i)
... return i*2
... compute = CachedCall(compute)
...
>>> t1=time.time();LongWait().compute(3);print time.time()-t1
6
3.01400005817
>>> t1=time.time();LongWait().compute(3);print time.time()-t1
6
0.00999999046326
>>>
(Need to use the function instead of a class with __call__
so that the method gets bound correctly. And I believe
this won't work if the class is derived from object. Nope!
I'm wrong. It does work.)
Andrew
dalke@...
--
http://mail.python.org/mailman/listinfo/python-list