I want to pickle an object and a second object that references the first. When I naively pickle/unpickle the two objects, the reference becomes a copy. How do I preserve the link between the two objects foo and bar.foo_ref?
import pickle
class Foo(object):
pass
foo = Foo()
bar = Foo()
bar.foo_ref = foo
with open('tmp.pkl', 'wb') as f:
pickle.dump(foo, f)
pickle.dump(bar, f)
with open('tmp.pkl', 'rb') as f:
foo2 = pickle.load(f)
bar2 = pickle.load(f)
print id(foo) == id(bar.foo_ref) # True
print id(foo2) == id(bar2.foo_ref) # False
# want id(foo2) == id(bar2.foo_ref)
My previous answer was missing your point. The problem with your code is that you’re not using the
PicklerandUnpicklerobjects. Here’s a working version with multiple dump calls: