propcache
propcache is a Python library that provides accelerated implementations of cached properties, enhancing performance for property caching in Python 3.9 and above. The current version is 0.4.1, with a release cadence that includes regular updates and bug fixes.
Warnings
- breaking Version 0.4.0 was yanked due to a reference leak caused by improper reference counting in Cython.
- gotcha propcache's under_cached_property uses self._cache instead of self.__dict__ to store cached values, which may lead to unexpected behavior if self._cache is not properly managed.
Install
-
pip install propcache
Imports
- cached_property
from propcache import cached_property
- under_cached_property
from propcache import under_cached_property
Quickstart
from propcache import cached_property
class MyClass:
def __init__(self, value):
self._value = value
@cached_property
def computed_property(self):
# Expensive computation
return self._value * 2
obj = MyClass(10)
print(obj.computed_property) # Outputs: 20