Cached property (translate Python -> D)

Nicholas Wilson iamthewilsonator at hotmail.com
Tue Jan 29 15:29:20 UTC 2019


On Tuesday, 29 January 2019 at 15:12:37 UTC, Victor Porton 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
> ---

Please use the learn forum for such questions. Thanks!


More information about the Digitalmars-d mailing list