I want to save object state and reuse it after some time.
I got some example on How to save object (Pickle module), I was not able to find how to resume class/method from the save state and proceed further.
Like game, we can save the game and latter we can continue, I know in game we save all the data and game read the data and build the game.
I want to save complete object and when I restore it it start working from saved state.
For example
class Test(objet):
def doSomeWork(self):
index = 0
while index < 99999:
print index
index += 1
if saveCondition:
# suppose when condition become True index value is 100
saveCondition = None
saveTheObjectToFile() # this might be saved in file
restoreClassObject = getSavedObject() # get object from file
# Now this should start printing from 100
# I want to resume the object state when it was saved.
The easiest way is to make sure that the state of the Object is not saved in local scopes, but as attributes of the object. Consider
Of course
doSomeWorkmakes no sense as such, but for the sake of the example imagine this method was a Thread so you could still interact with the object by setting it’srestoreClassObject.saveCondition = Trueto save it in the next iteration step.