Cached property (translate Python -> D)

Victor Porton porton at narod.ru
Tue Jan 29 15:12:37 UTC 2019


I want to translate if possible the following from Python to D. 
Please help.

---
class cached_property(object):
     """A version of @property which caches the value.  On access, 
it calls the
     underlying function and sets the value in `__dict__` so 
future accesses
     will not re-call the property.
     """
     def __init__(self, f):
         self._fname = f.__name__
         self._f = f

     def __get__(self, obj, owner):
         assert obj is not None, 'call {} on an 
instance'.format(self._fname)
         ret = obj.__dict__[self._fname] = self._f(obj)
         return ret
---


More information about the Digitalmars-d mailing list