I want to have a function that can use functools.lru_cache
, but not by default. I am looking for a way to use a function parameter that can be used to disable the lru_cache
. Currently, I have a two versions of the function, one with lru_cache
and one without. Then I have another function that wraps these with a parameter that can be used to control which function is used
def _no_cache(foo):
print('_no_cache')
return 1
@lru_cache()
def _with_cache(foo):
print('_with_cache')
return 0
def cache(foo, use_cache=False):
if use_cache:
return _with_cache(foo)
return _no_cache(foo)
Is there a simpler way to do this?