Python pickling keep object identity
问题 Is there any way to preserve the identity of a pickled object, i.e. have the below print True : import pickle class Foo: pass x = Foo() print(x is pickle.loads(pickle.dumps(x))) #False I am using cPickle and cpython 3.x on a Linux box, don't need something that's portable. 回答1: yes, it is possible; You'll need to include the "identity" in the pickled result some how; the most natural being to use __getnewargs__ and have a __new__ method return the existing, cached instance in that case.