cached_property_based_on#
- sunpy.util.cached_property_based_on(attr_name)[source]#
A decorator to cache the value of a property based on the value of a different class attribute.
When the decorated property is accessed,
getattr(instance, attr_name)is compared against its value when the property was last computed. If they are equal the cached value of the property is returned, otherwise the property is recomputed, cached, and the new value returned.- Parameters:
attr_name (
str) – The name of the attribute that changes are checked against. It is read usinggetattr(instance, attr_name).
Notes
Each property is cached together with the attribute value it was computed from, under
instance.__dict__["_cached_properties"]. Properties based on the same attribute are therefore invalidated independently of each other.If
getattr(instance, attr_name)returnsNonethe property is always recomputed and nothing is cached. This is because some attributes (e.g.MetaDict.item_hash) returnNoneto indicate that their value could not be determined (for example, because the underlying data contains an unhashable value), in which case equality between twoNonevalues must not be taken to mean that nothing has changed.