Cache_clear is to lru_cache as ? is to cached_property

Anyone else have a cached_property that they want to re-create/re-run periodically after several accesses of the computed and cached value?

My use case is that I read and cache data then access the cached data several times. I flag the data for change, later get an external notification of the change being enacted and only then need to re-read/re-cache the updated data.

There are work-arounds, but thought i’d advertise a use. Thanks :slight_smile:

Is this topic about improving Python the Language, or just using it?

The documentation states that you can clear the cache by deleting the attribute, ie del obj.prop.

import functools

class Spam:
    _eggs = 0

    @functools.cached_property
    def eggs(self):
        self._eggs += 1
        return self._eggs

spam = Spam()
spam.eggs  # 1
spam.eggs  # 1
del spam.eggs
spam.eggs  # 2

Improve by making more orthogonal .

Doh. It is stated right there in the docs as you stated! - I can’t read cos I already know and what I knew was b*llocks.

In [1]: from functools import cached_property
   ...: 
   ...: def life():
   ...:     print("Some expensive calc...")
   ...:     return 42
   ...: 
   ...: class Universe:
   ...:     
   ...:     @cached_property
   ...:     def everything(self):
   ...:         return life()

In [2]: u = Universe()

In [3]: u.everything
Some expensive calc...
Out[3]: 42

In [4]: u.everything
Out[4]: 42

In [5]: u.everything
Out[5]: 42

In [6]: del u.everything

In [7]: u.everything
Some expensive calc...
Out[7]: 42

In [8]: u.everything
Out[8]: 42

In [9]: u.everything
Out[9]: 42

In [10]: