把一个耗时的函数放到lazy_object_proxy.Proxy对象中,函数只在第一次调用时进行计算,后面需要调用时可以直接返回结果
参考:https://www.helplib.com/GitHub/article_118716
import lazy_object_proxy
def expensive_func():
from time import sleep
print('starting calculation')#just as example for a very slow computation
sleep(2)
print('finished calculation')
# return the result of the calculation
return 10
obj = lazy_object_proxy.Proxy(expensive_func)
#function is called only when object is actually used
print(obj) # now expensive_func is called
print(obj) # the result without calling the expensive_func