4 ms·
Useful example, adding memoization without rewriting the actual function, just add @decoratorname above the function. class memoize: def __init__(self, fun
by schtog 18y ago
Useful example, adding memoization without rewriting the actual function, just add @decoratorname above the function.
class memoize:
def __init__(self, function):
self.function = function
self.memoized = {}
def __call__(self, *args):
try:
return self.memoized[args]
except KeyError:
self.memoized[args] = self.function(*args)
return self.memoized[args]
@memoize
def fib(n)
- anamax 18y ago2.5 supported function decorators, which this is. (The decorator has always been a callable, so the above class has always been legal.) 2.6 supports decorators for other things, including classes.