Property Cached
The `property-cached` library (version 1.6.4) provides a Python decorator to cache the result of a class property's first access. Subsequent accesses to that property on the same instance retrieve the cached value without re-computation. It is a maintained fork of the original `cached-property` library, offering improved compatibility and type hinting support for modern Python versions. Releases are typically driven by bug fixes and compatibility updates.
Common errors
-
ModuleNotFoundError: No module named 'property_cached'
cause The `property-cached` library is not installed in your current Python environment.fixInstall the library using pip: `pip install property-cached` -
AttributeError: 'MyClass' object has no attribute 'my_property_name'
cause This error often occurs if the `@cached_property` decorator was either omitted or applied incorrectly above the method definition, or if the method name was misspelled.fixEnsure the decorator is correctly placed directly above the property method, for example: `@cached_property\ndef my_property_name(self): ...` -
TypeError: cached_property() takes 1 positional argument but 2 were given
cause This error typically indicates that `cached_property` was called as a function with parentheses (e.g., `@cached_property()`) without the method being passed, or that it was applied to a method that expects arguments other than `self`.fixUse `cached_property` directly as a decorator without parentheses (`@cached_property`), and only apply it to methods that act as properties (i.e., take only `self` as an argument).
Warnings
- gotcha The `cached_property` decorator caches the result indefinitely for the lifetime of the instance. If the underlying data or state used to compute the property changes, the cached value will become stale. It will not automatically re-evaluate.
- breaking This library is a fork of the original `cached-property`. If migrating from the original library, ensure all import statements are updated to `from property_cached import cached_property` to avoid mixing implementations or using an outdated version.
- gotcha `cached_property` is designed for properties (methods taking only `self`). It will not work correctly as a general-purpose memoization decorator for methods that require additional arguments.
Install
-
pip install property-cached
Imports
- cached_property
from cached_property import cached_property
from property_cached import cached_property
Quickstart
from property_cached import cached_property
import time
class MyResource:
def __init__(self, name):
self.name = name
self._heavy_computation_count = 0
@cached_property
def loaded_data(self):
print(f"[{self.name}] Performing heavy computation for data...")
time.sleep(0.1) # Simulate delay
self._heavy_computation_count += 1
return f"Data for {self.name} (computed {self._heavy_computation_count} times)"
resource1 = MyResource("Resource A")
print(f"First access: {resource1.loaded_data}")
print(f"Second access: {resource1.loaded_data}") # Will not re-compute
print("\nModifying resource, then trying to access again...")
# If underlying state changes, the cached property remains stale
resource1.name = "Resource A (modified)"
print(f"Access after name change: {resource1.loaded_data}") # Still uses old cached value
print("\nDeleting cached property to force re-computation...")
del resource1.loaded_data
print(f"Access after deletion: {resource1.loaded_data}") # Re-computes with new name