0

At this point, I have found several python implementations for lazily-evaluated property/attribute (also known as cached method), these include:

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.

tribbloid
  • 4,026
  • 14
  • 64
  • 103
  • 3
    Lazily evaluated and cached are not at all the same thing. – Daniel Roseman Oct 19 '19 at 16:48
  • 2
    What exactly is that you're trying to achieve? Lazy generation of values can be done a variety of ways (a lot of ways actually), depends on what exactly is the intent. A generator is the first thing that comes to mind, but it ain't no silver bullet so knowing the intent definitely helps here. Also, python will do eager evaluation of all function arguments, no matter what (you can trick it, by passing functions as arguments..), not sure if its what you mean by lazy evaluation. – Pedro Rodrigues Oct 19 '19 at 16:52
  • 1
    _Since python doesn't have the concept of object/singleton_ - don't understand this point. Python has the concept of objects (in fact, in Python this concept is implemented in 100% - everything is an object). Also Python has the concept of singleton and this pattern can be implemented in multiple ways (decorators, classes, metaclasses, etc.). – sanyassh Oct 19 '19 at 16:57
  • 1
    Can't you just slap [`functools.lru_cache`](https://docs.python.org/3/library/functools.html#functools.lru_cache) on your function? – Aran-Fey Oct 19 '19 at 16:59
  • 1
    Maybe you should post an example of code showing what you want to achieve to make your question more clear. – sanyassh Oct 19 '19 at 17:00
  • sorry the wording is indeed problematic, it should be 'package object' as like in scala (where an object can be used interchangeably with a package) – tribbloid Oct 19 '19 at 17:02
  • You can put *anything* in `sys.module`, even if it's not an instance of `types.ModuleType`, and `import` will still work. – o11c Oct 19 '19 at 17:16
  • in my example my function is defined in the same package, so theoretically I shouldn't use `import` at all – tribbloid Oct 19 '19 at 18:38

0 Answers0