Storing serialized object in database then restoring object state from key
<p>Alright, this is pretty fun.</p>

<p>Code will illustrate best what I'm thinking about:</p>

<pre><code>class Restorable(object):
    def __init__(self, a=1, b=1):
        self.a = a
        self.b = b

    def restore(self, serialized_object):
        # I'm aware this is local, just illustrating
        self = pickle.loads(serialized_object)

r = Restorable()
r.a = 1000
r.b = 2000
saved_state = pickle.dumps(r)
r.a = -1
r.b = -1
r.restore(pickle.dumps(saved_state)
# r.a should again be 1000
# r.b should again be 2000
</code></pre>

<p>The idea is: would be possible to, say, <code>pickle.dumps()</code> an object, store it somewhere, modify the real object and later on restore it to its previous state through what I've safely stored?</p>

<p>I've tried the code I've shown above, it does <strong>temporarily</strong> set <code>self</code> to the previous state, but only locally - well, no surprise here.</p>

<p>This is a curiosity question.</p>

<p>EDIT: To formulate the question in a clear way:</p>

<p>Is it possible to write an object that would restore its state using a serialized object of the same type?</p>

<p>Actually, I see this can be done by creating an instance of the serialized object inside <code>restore()</code> and copying values one by one. But this could be tedious for large objects.</p>
