getstate, setstate and pickle

Greg Ewing greg.ewing at compaq.com
Sun Jun 27 20:24:39 EDT 1999


Emile van Sebille wrote:
> 
> The script below illustrates that dumps(r) ==dumps(loads(dumps(r))) but
> that r <> loads(dumps(r)).

If you haven't given your class a __cmp__ method,
the == will be comparing object references, which
will be different even if the two objects have an
identical structure.

To do a "deep comparison" of two instances, they
need a __cmp__ method which does the appropriate
things -- and so on recursively for any other
objects which are part of the structure.

If you really do want dumps(loads(dumps(r))) to
return a reference to the original r, then you're
out of luck -- pickle won't do that. It will
give you a deep copy of r instead.

Greg




More information about the Python-list mailing list