Preserving object's original address when restoring from shelf

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Jun 27 04:39:57 EDT 2000


wolf at one.net writes:

> Am I being clueless here? Is there some simple way to restore objects
> that doesn't invoke this problem?

That is a hard problem; when restoring an object from a shelf, it
always creates a new object. With the old object still existing, you
cannot expect the two objects to have the same identity.

If all you want to restore is the state of the object, perhaps it is
easier to copy the state of the restored object to the existing
object:

   oldobj = __GameModule__.__dict__[VariableName]
   newobj = SavedGame[VariableName]
   oldobj.__dict__ = newobj.__dict__

Please note that this indeed replaces the object's dictionary. You may
need to update the state from the saved game to the existing object
more selectively than that.

Also, you better throw-away the newobj afterwards, as you now have two
objects sharing the same state. Perhaps

  oldobj.__dict__.update(newobj.__dict__)

is more appropriate - it keeps the old dictionary, and only copies
keys and values.

Regards,
Martin



More information about the Python-list mailing list