Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with hasattr()
? I worked out how to make it lazy, but hasattr()
makes it evaluate prematurely. E.g.,
class lazyattribute:
# Magic.
class A:
@lazyattribute
def bar(self):
print("Computing")
return 5
>>> a = A()
>>> print(a.bar)
'Computing'
5
>>> print(a.bar)
5
>>> b = A()
>>> hasattr(b, 'bar')
'Computing'
5
# Wanted output: 5