Just del it as documentation says. It will lead to recalculation on next access.
class SomeClass(object):
@cached_property
def expensive_property(self):
return datetime.now()
obj = SomeClass()
print obj.expensive_property
print obj.expensive_property # outputs the same value as before
del obj.expensive_property
print obj.expensive_property # outputs new value
For Python 3 it's the same use of del
. Below is an example of a try/except block.
try:
del obj.expensive_property
except AttributeError:
pass