Cached property (translate Python -> D)

Daniel Kozak kozzi11 at gmail.com
Tue Jan 29 15:26:13 UTC 2019


Maybe https://dlang.org/phobos/std_functional.html#memoize should helped you

On Tue, Jan 29, 2019 at 4:15 PM Victor Porton via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> 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
> ---
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20190129/3b1ccca0/attachment.html>


More information about the Digitalmars-d mailing list