Functional Classes

functools.cached_property()

Compute a value when an instance attribute is first accessed and store it in its writable instance dictionary. Deleting the attribute permits recalculation; concurrent getters can run more than once.

Formal Syntax & Structure

python syntaxpython
functools.cached_property(func)

Parameters & Attributes

NameTypeDescription
funccallableCallable applied by the operation.

Return Value

descriptor

The descriptor result described above.

Working Example

Example Snippetpython
import functoolsclass Basket: calls=0 @functools.cached_property def total(self):  self.calls+=1  return 6obj=Basket()print((obj.total,obj.total,obj.calls))
Python Reference