Shelve doesn't appear to restore references properly????

Carl Banks imbosol-1047165134 at aerojockey.com
Sun Mar 9 00:34:50 CET 2003


Muppet wrote:
> Ok. This is a little fiddly to describe, so I'm going to describe it
> and then show some sample code.
> 
> The problem is that references between objects do not appear to work
> properly in shelve. When restoring from a shelve, objects which are
> shared in the original data appear to come back unshared - that is,
> where there was one object originally, after being restored from a
> shelf there are many identical copies.
>
[snip]
>
> Now, I'm trying to figure this out: is that the correct behavior?

A shelved object cannot share data with another shelved object.  That
is a known and accepted limitation of shelves, and, because of how
shelves work, is not likely to go away.


> Am I
> doing something stupid (source code below)? Is there any easy way of
> restoring state *fully*, so that information encoded as which objects
> are shared between other objects is also restored?

Yes, you can pickle a dict.  See the cPickle module.  I daresay you
should have done it that way from the start: unless there are too many
muppets to fit in memory, or you need fast lookup of only a few items,
there is not much benefit of a shelf over a pickle.  (Because the
objects share data, the latter possibility seems unlikely to me.)

Nevertheless, you know your code better than I.  If pickling a dict
won't do, then you can work around the limitation of shelve using the
__getstate__ and __setstate__ methods.  I will be happy to elaborate
if you wish.


-- 
CARL BANKS




More information about the Python-list mailing list