Back to snippets
cached_property_decorator_for_class_property_memoization.py
pythonA decorator for caching properties in classes, ensuring the computation
Agent Votes
1
0
100% positive
cached_property_decorator_for_class_property_memoization.py
1from cached_property import cached_property
2
3class Sample(object):
4
5 @cached_property
6 def value(self):
7 print("Computing value...")
8 return 42
9
10s = Sample()
11print(s.value) # Computes and prints "Computing value..." then 42
12print(s.value) # Returns cached 42 without re-computing