Python 3.8 adds a great decorator to functools: cached_property
.
Technically all required APIs were available since Python 3.6, but
releases until 3.8 don't have it and users need to live with this reality.
Therefore, this package backports said feature to Python 3.6 and 3.7.
Simply do
from backports.cached_property import cached_property
and it will work as documented upstream (see copy below).
The implementation of our cached_property
backport is very close to that
of the future Python 3.8 standard library.
For Python >= 3.8, the actual cached_property
from functools is passed
through, of course.
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.
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)
This 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 (since the__dict__
attributes on type instances are read-only proxies for the class namespace), and those that specify__slots__
without including__dict__
as one of the defined slots (as such classes don't provide a__dict__
attribute at all).