To add memoization to functions in Python, the functools.lru_cache()
decorator can be used:
import functools
@functools.lru_cache(maxsize=None)
def my_func():
...
However, I would like to define the following alias to the above use of functools.lru_cache()
to make the code more readable:
@memoize
def my_func():
...
My question is: how to define such an alias? The following piece of code doesn't work:
memoize = functools.partial(functools.lru_cache, maxsize=None)
You don't need the functools.partial
because it is already setup to take two calls. Just call it once:
memoize = functools.lru_cache(maxsize=None)
then use the alias as a decorator:
@memoize
def my_func():
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With