How to store the result of an executed function and re-use later?
python, python-2.7
Solution
def readDb():
... #Fetch a lot of data from db, spends a lot time
return aList
def calculation(data):
x=data
...process x...
return y
data = readDb()
calculation(data)
calculation(data)
calculation(data)
This will only hit the database once.
Basically, you want to save the results of readDb() to a seperate variable which you can then pass to calculation().
Problem
E.g., I have: ``` def readDb(): # Fetch a lot of data from db, spends a lot time ... return aList def calculation(): x = readdb() # Process x ... return y ``` In the python interpreter, each time I run `calculation()` it takes a lot of time to re-read the database, which is unnecessary. How can I store the result from `readdb()` to avoid this reducdant process? Edit: I found a similar question here but I don't quite know the answer Save functions for re-using without re-execution