-1

I have a problem understanding how to apply the @lru_cache decorator properly. I have a dictionary attribute that computes values by calling another function on a list of values. I want to cache any access to attribute. Is there any way to declare the lru_cache invalid whenever there is a write to some_list? So far, the code is as follows:

@property
@lru_cache(maxsize=None)
def attribute(self):
    attribute = {}
    for k in self.some_dict:
        attribute[k] = self.complex_computation(self.some_list)

    return attribute

1 Answers1

-1

It would be better to use @lru_cache for your self.complex_computation method.

@lru_cache saves function_calls, which is, basically function + args + return_value. So there must be a dependency between return value and args, but property doesn't have this dependency - you just always pass the same self here.

But self.complex_computation has such a parameter and it's really more suitable for the @lru_cache decorator. But rememeber - @lru_cache does not work for List arguments.