How to store the result of an executed function and re-use later?

后端 未结 3 1507
悲哀的现实
悲哀的现实 2021-01-19 03:16

E.g., I have:

def readDb():
    # Fetch a lot of data from db, spends a lot time
    ...
    return aList

def calculation():
    x = readdb()
    # Process          


        
3条回答
  •  隐瞒了意图╮
    2021-01-19 03:41

    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().

提交回复
热议问题