I work with two functions, in the first one I want to get actual data, for example, about the value of a currency at the minute I launched it. And in the second function to work with those once received data.

I will give an example with the time function

import time def stop_time(): t = time.time() return t def my_data(old_time): print(int(time.time()) -int(old_time) ) for _ in range(3): time.sleep(2) my_data(old_time=stop_time()) 

I want the first function to remember the time, and the second function to count how many seconds have passed, subtracting the saved time from the current one. But this record is incorrect, it always calls the current time from the stop_time () function.

    2 answers 2

    This is called memoization and in the standard library there is a tool for this.

     from functools import lru_cache @lru_cache() def stop_time(): t = time.time() return t 

      Not so easy?

       start_time = time.time() for _ in range(3): time.sleep(2) my_data(old_time=start_time) 
      • The question is just an example. In fact, a function can contain 150 lines of complex code and be called in a dozen different places. - Sergey Gornostaev