cached_getter

lsst.utils.classes.cached_getter(func: Callable[[_S], _R]) Callable[[_S], _R]

Decorate a method to cache the result.

Only works on methods that take only self as an argument, and returns the cached result on subsequent calls.

Parameters:
funcCallable

Method from which the result should be cached.

Returns:
Callable

Decorated method.

Notes

This is intended primarily as a stopgap for Python 3.8’s more sophisticated functools.cached_property, but it is also explicitly compatible with the immutable decorator, which may not be true of cached_property.

cached_getter guarantees that the cached value will be stored in an attribute named _cached_{name-of-decorated-function}. Classes that use cached_getter are responsible for guaranteeing that this name is not otherwise used, and is included if __slots__ is defined.