I am writing a program that should run more or less continously. If I terminate the program, or if it throws an error, I want all object instances to be saved. To do so, I save all instances using jsonpickle. When the program continues, I want to continue exactly where I left off. My idea was to do something like the following:
class A(object):
def __init__(self):
try:
with open('A.json', 'r') as fp:
self = jsonpickle.decode(json.load(fp))
except IOError:
self.X = 'defaultvalue'
self.Y = 'defaultvalue'
Where A.json contains an instance of A previously saved using jsonpickle (that part works). However, self is not overwritten by my code.
I suspect that I have to implement what I want to do in __new__. I read the documentation, however I am a bit lost. I would appreciate any advice on how to implement what I want in a good way.