Skip to content

Commit

Permalink
ENH: created a cached_property decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
giovaniceotto committed Nov 12, 2022
1 parent ce5a730 commit d24fce7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rocketpy/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
_NOT_FOUND = object()


class cached_property:
def __init__(self, func):
self.func = func
self.attrname = None
self.__doc__ = func.__doc__

def __set_name__(self, owner, name):
self.attrname = name

def __get__(self, instance, owner=None):
if instance is None:
return self
cache = instance.__dict__
val = cache.get(self.attrname, _NOT_FOUND)
if val is _NOT_FOUND:
val = self.func(instance)
cache[self.attrname] = val
return val

0 comments on commit d24fce7

Please sign in to comment.