3 ms·
it is a particular part of the Python 3 standard library. it is one extra line of code to automatically memoize a recursive function. you just do: from fu
by currymj 6y ago
it is a particular part of the Python 3 standard library. it is one extra line of code to automatically memoize a recursive function.
you just do:
from functools import lru_cache
@lru_cache(maxsize=None)
def brute_recursive_func(x):
...
And you're good to go.
- rmk 6y agoVery nice! I often forget this handy thing during interviews. Good thing to impress your interviewer with :)
- rmk 6y agoOne thing of note: The arguments to the method you are memoizing need to be hashable. For example, if you want to memoize a method call whose parameters include a list, you will get an error!