-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy.py
37 lines (30 loc) · 911 Bytes
/
lazy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#coding: utf-8
# Property that is only computed once
def lazyproperty(f):
@property
def wrapper(self,*args,**kwargs):
if not hasattr(self,'_'+f.__name__):
setattr(self,'_'+f.__name__,f(self,*args,**kwargs))
return getattr(self,'_'+f.__name__)
return wrapper
if __name__ == '__main__':
def mysum(l): # Example function to be called in my test getter
print("Summing...")
return sum(l)
class Stuff():
def __init__(self,data=[0,1,2]):
self.data = data
# s.sum will be computed on the first time we get it
# but the same value will be returned on every other call
# Simply delete self._sum to mark it as to be computed again
@lazyproperty
def sum(self):
return mysum(self.data)
s = Stuff()
print("First call:")
print("sum=",s.sum)
print("second call:")
print("sum=",s.sum)
del s._sum
print("Third call:")
print("sum=",s.sum)