`backports.cached_property` is a Python library that provides a backport of the `functools.cached_property` decorator, which was introduced in Python 3.8. It allows a method of a class to be transformed into a property whose value is computed only once per instance and then cached as a regular attribute. This is particularly useful for expensive computed properties of instances that are otherwise effectively immutable. The current version is 1.0.2, and it appears to be in maintenance mode, as its primary purpose is to backport a feature now in the standard library.
pip install backports-cached-propertyVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `cached_property` decorator. The `stdev` and `variance` methods are decorated, meaning their values are computed only on the first access and then cached. Subsequent accesses retrieve the cached value without re-executing the method.
If running on Python 3.8 or higher, change `from backports.cached_property import cached_property` to `from functools import cached_property`.
Avoid using `cached_property` on metaclasses or classes that use `__slots__` without explicitly including `__dict__` in the `__slots__` definition.
If the state relevant to a cached property changes, explicitly `del` the cached attribute to force re-evaluation on the next access.
For Python 3.6 and 3.7, you need to install the `backports.cached_property` library and import it from there. ```python # First, install the backport library # pip install backports.cached_property # Then, import from the backport from backports.cached_property import cached_property ```
You need to install the library using pip. ```bash pip install backports.cached_property ```
If you need a mutable property, use the standard `@property` decorator with a `@property.setter`. If using `__slots__`, either avoid `cached_property` or ensure `__dict__` is included in `__slots__` if you absolutely need to use `cached_property` with slotted classes. To clear a cached value, `del instance.property_name` to force re-evaluation on next access.
```python
# Example for setter
class MyClass:
def __init__(self, value):
self._value = value
@property
def my_property(self):
return self._value
@my_property.setter
def my_property(self, new_value):
self._value = new_value
# Example for __slots__ (avoiding cached_property or including __dict__)
class SlottedClassWithDict:
__slots__ = ('_data', '__dict__') # Include __dict__ explicitly
def __init__(self, data):
self._data = data
from backports.cached_property import cached_property
@cached_property
def computed_value(self):
print("Computing value...")
return self._data * 2
```If you are running Python 3.8 or a newer version, you should switch your import statement to use the standard library version. ```python # Change this: # from backports.cached_property import cached_property # To this for Python 3.8+: from functools import cached_property ```
No dependency data recorded yet.