cached_property() - computed once per instance, cached as attribute
Project Links
Meta
Author: Aleksei Stepanov
Maintainer: Aleksei Stepanov penguinolog@gmail.com
Requires Python: >=3.6.0
Classifiers
Development Status
- 5 - Production/Stable
Intended Audience
- Developers
Topic
- Software Development :: Libraries :: Python Modules
License
- OSI Approved :: MIT License
Programming Language
- Python :: 3
- Python :: 3.6
- Python :: 3.7
- Python :: 3.8
- Python :: 3.9
- Python :: Implementation :: CPython
- Python :: Implementation :: PyPy
What
Python 3.8 adds great descriptor to functools: cached_property. Technically all required APIs was available since python 3.6, but it is what it is.
This package is a backport of this functionality for python 3.6 and 3.7.
How to use
from backports.cached_property import cached_property
And then python 3.8 documentation will work (because code is minimally changed):
Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance. Similar to property, with the addition of caching. Useful for expensive computed properties of instances that are otherwise effectively immutable.
Example:
class DataSet:
def __init__(self, sequence_of_numbers):
self._data = sequence_of_numbers
@cached_property
def stdev(self):
return statistics.stdev(self._data)
@cached_property
def variance(self):
return statistics.variance(self._data)
1.0.2
Jun 14, 2022
1.0.1
Feb 04, 2021
1.0.0.post2
May 13, 2020
1.0.0.post1
Apr 07, 2020
1.0.0.post0
Apr 07, 2020
1.0.0
Apr 07, 2020
Wheel compatibility matrix
Files in release
Extras:
None
Dependencies:
typing
(>=3.6)