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.