1

I'm using the following decorator for lazy evaluation of a property (based on this and this questions):

from functools import wraps
def lazy_property(fn):
    name = '_lazy_' + fn.__name__

    @property
    @wraps(fn)
    def wrapper(self):
        if not hasattr(self, name):
            setattr(self, name, fn(self))
        return getattr(self, name)

    return wrapper

Then within some class I have mixed regular and "lazy" properties:

class A(object):
    @property
    def f1(self):
        return 1

    @lazy_property
    def f2(self):
        return 2

    @lazy_property
    def f3(self):
        return 3

Is there any way how I can figure out that f1 is a regular and f2 and f3 are lazy properties without calling them? For the methods, @wraps(fn) should set up the __wrapped__ attribute, but it does not happen for the properties.

I need this to write some "pre-fetching" method, which will evaluate all "lazy" properties at once (i.e. cache all necessary data). Indeed the "brute force solution" would be to hardcode all the names as a list, but this would not be easy to maintain in the future development - when new lazy-or-not properties will be added and/or types of properties will be changed based on the better understanding of use-cases.

Vladimir
  • 1,363
  • 2
  • 14
  • 28
  • It can be done fairly easily if you are willing to use a custom property class such as the one provided by @Cyclone in the answer you [linked](https://stackoverflow.com/a/6849299/3895264) in the question – FujiApple Nov 24 '17 at 03:55

0 Answers0