-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoization.py
More file actions
41 lines (29 loc) · 1.03 KB
/
memoization.py
File metadata and controls
41 lines (29 loc) · 1.03 KB
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
38
39
40
41
import functools
class MemoizeFirstCall(object):
"""
To be used only for objects that are created once and never change.
WARNING: Ignores completely differences in args and kwargs after initial memoization.
"""
def __init__(self, func):
self.func = func
functools.update_wrapper(self, func)
self.storage_used = False
self.stored_value = None
def __call__(self, *args, **kwargs):
if not self.storage_used:
self.storage_used = True
self.stored_value = self.func(*args, **kwargs)
return self.stored_value
if __name__ == '__main__':
import time
if 1:
@MemoizeFirstCall
def f(x, y):
delay = 2
print('\nCalling {}.. delay {} seconds'.format(f.__name__, delay))
print('(First call should be delayed. Following calls should be instant.)')
time.sleep(delay)
return 'x was {}, y was {}'.format(x, y)
print(f(x=1, y=2))
print(f(1, 5))
print(f())