I'm trying wrap my head around python decorators so I borrowed this supposedly fastest memoization implementation :
class memoize(dict):
def __init__(self, func):
self.func = func
def __call__(self, *args):
return self[args]
def __missing__(self, key):
result = self[key] = self.func(*key)
return result
>>> @memoize
... def foo(a, b):
... return a * b
>>> foo(2, 4)
8
>>> foo
{(2, 4): 8}
Piece of cake but then I came to know about functools wraps. The doc says it:
This is a convenience function for invoking update_wrapper() as a function decorator when defining a wrapper function. It is equivalent to partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated).
When to use it and when not to use it ? Could anyone tell me If I've to use the above class memoization
using that functools.wraps how would I change it ? Thanks in advance!