I want to change the value of a variable on exit so that on the next run, it remains what was last set. This is a short version of my current code:
def example():
x = 1
while True:
x = x + 1
print x
On ‘KeyboardInterrupt’, I want the last value set in the while loop to be a global variable. On running the code next time, that value should be the ‘x’ in line 2. Is it possible?
This is a bit hacky, but hopefully it gives you an idea that you can better implement in your current situation (
pickle/cPickleis what you should use if you want to persist more robust data structures – this is just a simple case):