Skip to content

Commit d24fce7

Browse files
committed
ENH: created a cached_property decorator
1 parent ce5a730 commit d24fce7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rocketpy/tools.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
_NOT_FOUND = object()
2+
3+
4+
class cached_property:
5+
def __init__(self, func):
6+
self.func = func
7+
self.attrname = None
8+
self.__doc__ = func.__doc__
9+
10+
def __set_name__(self, owner, name):
11+
self.attrname = name
12+
13+
def __get__(self, instance, owner=None):
14+
if instance is None:
15+
return self
16+
cache = instance.__dict__
17+
val = cache.get(self.attrname, _NOT_FOUND)
18+
if val is _NOT_FOUND:
19+
val = self.func(instance)
20+
cache[self.attrname] = val
21+
return val

0 commit comments

Comments
 (0)