At this point, I have found several python implementations for lazily-evaluated property/attribute (also known as cached method), these include:
one utility class in torch library: torch.distributions.utils import lazy_property
And many more. All of them have short implementation and can be used as decorators on class methods. However, they also share the same limitation: they cannot be used on function OUTSIDE a class.
This is an example:
from torch.distributions.utils import lazy_property
@lazy_property
def lazyFn():
return 3
v = lazyFn
type(v)
this short program will yield the result:
<torch.distributions.utils.lazy_property at ...
instead of the correct one 3
Since python doesn't have the concept of package object (or singleton object), this makes the use of lazy evaluation severely limited. I'm wondering if there could be an equally simple implementation that can supports this, or it is impossible due to engine limitation?
UPDATE: I'm also looking for an implementation in python that can support similar use case of @property outside of a class, with no avail. The use case is important as some large scale code refactoring requires me to alternative between a function and variable without affecting its interface signature. (or to put it simpler: I want to call a function without using brackets)
I'm using Python 3.8 at the moment.