{"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":"python","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,"search_vec":"'1.0.2':71 '3.8':23 'allow':25 'appear':74 'attribut':51 'backport':12,86,94 'backports.cached':1,3 'cach':47,95 'class':30 'comput':40,58 'current':68 'decor':17,97 'effect':65 'expens':57 'featur':88 'functools.cached':15 'immut':66 'instanc':44,61 'introduc':20 'librari':8,93 'mainten':78 'method':27 'mode':79 'otherwis':64 'particular':54 'per':43 'perform':96 'primari':82 'properti':2,4,16,36,59 'provid':10 'purpos':83 'python':7,22 'regular':50 'standard':92 'transform':33 'use':55 'valu':38 'version':69 'whose':37","created_at":"2026-04-14T01:20:27.590034+00:00","updated_at":"2026-04-15T23:42:43.830105+00:00","problems":[{"fix":"For Python 3.6 and 3.7, you need to install the `backports.cached_property` library and import it from there.\n\n```python\n# First, install the backport library\n# pip install backports.cached_property\n\n# Then, import from the backport\nfrom backports.cached_property import cached_property\n```","cause":"This error occurs when attempting to import `cached_property` from the `functools` module on Python versions older than 3.8, as `functools.cached_property` was introduced in Python 3.8.","error":"ImportError: cannot import name 'cached_property' from 'functools'"},{"fix":"You need to install the library using pip.\n\n```bash\npip install backports.cached_property\n```","cause":"This error indicates that the `backports.cached_property` library has not been installed in your Python environment.","error":"ModuleNotFoundError: No module named 'backports.cached_property'"},{"fix":"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.\n\n```python\n# Example for setter\nclass MyClass:\n    def __init__(self, value):\n        self._value = value\n\n    @property\n    def my_property(self):\n        return self._value\n\n    @my_property.setter\n    def my_property(self, new_value):\n        self._value = new_value\n\n# Example for __slots__ (avoiding cached_property or including __dict__)\nclass SlottedClassWithDict:\n    __slots__ = ('_data', '__dict__') # Include __dict__ explicitly\n    def __init__(self, data):\n        self._data = data\n\n    from backports.cached_property import cached_property\n    @cached_property\n    def computed_value(self):\n        print(\"Computing value...\")\n        return self._data * 2\n```","cause":"`cached_property` is designed for immutable values and does not support defining a setter like a regular `@property`. Additionally, it requires instances to have a mutable `__dict__` attribute to store the cached value, meaning it will not work with classes that use `__slots__` without explicitly including `__dict__` in the slots.","error":"AttributeError: 'cached_property' object has no attribute 'setter' OR cached_property not working with __slots__"},{"fix":"If you are running Python 3.8 or a newer version, you should switch your import statement to use the standard library version.\n\n```python\n# Change this:\n# from backports.cached_property import cached_property\n\n# To this for Python 3.8+:\nfrom functools import cached_property\n```","cause":"The `backports.cached_property` library is a backport for Python versions older than 3.8. On Python 3.8 and newer, the native `functools.cached_property` is available in the standard library and is generally preferred for performance and maintainability, leading to warnings or recommendations to switch.","error":"Warning: 'backports.cached_property' is deprecated (or similar message when using on Python 3.8+)"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.2","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/penguinolog/backports.cached_property","docs":null,"changelog":null,"pypi":"https://pypi.org/project/backports-cached-property/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":[],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}