# type: ignore import pickle, os # This is a demo for using pickle to store objects as files for later executions # This should work whether you're using sage or python, and similar things can # be done in other programming languages # In this demo we just store an integer, but we can store much more complex objects as well # Standin for some long running function def big_calc(): return 10 # We check whether there is a bigobj file already if os.path.isfile("bigobj"): # If the file exists we open it and use pickle to load its contents # to the variable N f = open("bigobj", "rb") # open bigobj in binary read mode N = pickle.load(f) else: # If the file doesn't exist we do the computation, create the file # and store the value of the variable into the file N = big_calc() f = open("bigobj", "wb") # open bigobj in binary write mode pickle.dump(N, f) # To recompute N if its value should change you can simply delete the bigobj file