File tree 1 file changed +14
-4
lines changed 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -62,16 +62,26 @@ The :mod:`functools` module defines the following functions:
62
62
Example::
63
63
64
64
class DataSet:
65
+
65
66
def __init__(self, sequence_of_numbers):
66
- self._data = sequence_of_numbers
67
+ self._data = tuple( sequence_of_numbers)
67
68
68
69
@cached_property
69
70
def stdev(self):
70
71
return statistics.stdev(self._data)
71
72
72
- @cached_property
73
- def variance(self):
74
- return statistics.variance(self._data)
73
+ The mechanics of :func: `cached_property ` are somewhat different from
74
+ :func: `property `. A regular property blocks attribute writes unless a
75
+ setter is defined. In contrast, a *cached_property * allows writes.
76
+
77
+ The *cached_property * decorator only runs on lookups and only when an
78
+ attribute of the same name doesn't exist. When it does run, the
79
+ *cached_property * writes to the attribute with the same name. Subsequent
80
+ attribute reads and writes take precedence over the *cached_property *
81
+ method and it works like a normal attribute.
82
+
83
+ The cached value can be cleared by deleting the attribute. This
84
+ allows the *cached_property * method to run again.
75
85
76
86
Note, this decorator interferes with the operation of :pep: `412 `
77
87
key-sharing dictionaries. This means that instance dictionaries
You can’t perform that action at this time.
0 commit comments