-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
.. _propcache-reference: | ||
|
||
============ | ||
Reference | ||
============ | ||
|
||
.. module:: propcache | ||
|
||
|
||
|
||
cached_property | ||
=============== | ||
|
||
.. decorator:: cached_property(func) | ||
|
||
This decorator functions exactly the same as the standard library | ||
:func:`cached_property` decorator, but it's available in the | ||
:mod:`propcache` module along with an accelerated Cython version. | ||
|
||
As with the standard library version, the cached value is stored in | ||
the instance's ``__dict__`` dictionary. To clear a cached value, you | ||
can use the :meth:`del` operator on the instance's attribute or call | ||
``instance.__dict__.pop('attribute_name', None)``. | ||
|
||
under_cached_property | ||
===================== | ||
|
||
.. decorator:: under_cached_property(func) | ||
|
||
Transform a method of a class into a property whose value is computed | ||
only once and then cached as a private attribute. Similar to the | ||
:func:`cached_property` decorator, but the cached value is stored | ||
in the instance's ``_cache`` dictionary instead of ``__dict__``. | ||
|
||
Example:: | ||
|
||
class MyClass: | ||
|
||
def __init__(self, data: List[float]): | ||
self._data = data | ||
self._cache = {} | ||
|
||
@cached_property | ||
def calculated_data(self): | ||
return expensive_operation(self._data) | ||
|
||
def clear_cache(self): | ||
self._cache.clear() | ||
|
||
instance = MyClass([1.0, 2.0, 3.0]) | ||
print(instance.calculated_data) # expensive operation | ||
|
||
instance.clear_cache() | ||
print(instance.calculated_data) # expensive operation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters