If you need more control, you can create a custom Python cache decorator:
def cached_property(func):
attr_name = f""_{func.__name__}""
@property
def wrapper(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, func(self))
return getattr(self, attr_name)
return wrapper
class Example:
@cached_property
def expensive_computation(self):
print(""Computing value..."")
return 42 # Simulate an expensive operation
This decorator stores the computed value as an instance attribute, preventing redundant computations.