{"id":5129,"library":"backports-cached-property","title":"backports.cached_property","description":"`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.","status":"maintenance","version":"1.0.2","language":"en","source_language":"en","source_url":"https://github.com/penguinolog/backports.cached_property","tags":["backport","caching","performance","decorator"],"install":[{"cmd":"pip install backports-cached-property","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"cached_property","correct":"from backports.cached_property import cached_property"}],"quickstart":{"code":"import statistics\nfrom backports.cached_property import cached_property\n\nclass DataSet:\n    def __init__(self, sequence_of_numbers):\n        self._data = sequence_of_numbers\n\n    @cached_property\n    def stdev(self):\n        # This computation will only run once per instance\n        print(\"Calculating standard deviation...\")\n        return statistics.stdev(self._data)\n\n    @cached_property\n    def variance(self):\n        # This computation will only run once per instance\n        print(\"Calculating variance...\")\n        return statistics.variance(self._data)\n\n\ndata = DataSet([1, 2, 3, 4, 5])\nprint(f\"Standard deviation: {data.stdev}\")\nprint(f\"Standard deviation (cached): {data.stdev}\")\nprint(f\"Variance: {data.variance}\")","lang":"python","description":"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."},"warnings":[{"fix":"If running on Python 3.8 or higher, change `from backports.cached_property import cached_property` to `from functools import cached_property`.","message":"For Python 3.8 and newer, `functools.cached_property` from the standard library should be used instead. This backport is only necessary for Python 3.6 and 3.7. Using the standard library version is generally preferred for performance and maintainability.","severity":"deprecated","affected_versions":"<3.8 (if using this backport for >=3.8)"},{"fix":"Avoid using `cached_property` on metaclasses or classes that use `__slots__` without explicitly including `__dict__` in the `__slots__` definition.","message":"The `cached_property` decorator requires that the `__dict__` attribute on each instance be a mutable mapping. This means it will not work with some types, such as metaclasses (where `__dict__` attributes on type instances are read-only proxies for the class namespace) or classes that specify `__slots__` without including `__dict__` as one of the defined slots (as such classes don't provide a `__dict__` attribute at all).","severity":"gotcha","affected_versions":"All versions"},{"fix":"If the state relevant to a cached property changes, explicitly `del` the cached attribute to force re-evaluation on the next access.","message":"`cached_property` values are cached for the life of the instance. If the underlying data that the property depends on changes, the cached property will not automatically re-evaluate. The cached value must be manually cleared by deleting the attribute (e.g., `del instance.property_name`) for it to be recomputed on next access.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}