Reputation: 2179
Why does pickle reuse the existing Python class 'C' instead of reconstructing the class from the pickled bytes? Is there a way I can pickle and unpickle without side effects?
Here's my repl session:
In [1]: import pickle
In [2]: class C:
...: pass
...:
In [3]: hasattr(C, 'foo')
Out[3]: False
In [4]: pickled = pickle.dumps(C)
In [5]: C.foo = 'bar'
In [6]: hasattr(C, 'foo')
Out[6]: True
In [7]: C_without_foo = pickle.loads(pickled)
In [8]: hasattr(C_without_foo, 'foo')
Out[8]: True
In [9]: hasattr(C, 'foo')
Out[9]: True
Upvotes: 3
Views: 119
Reputation: 15568
That is because they share the same identity, namely, they point to the same memory location. We have the same behaviour with list
a = [1, 2, 3]
b = a
print(id(a) == id(b)) # True
b.append(4)
print(a) # [1, 2, 3, 4]
c = a.copy()
a.append(5)
print(c) # [1, 2, 3, 4]
print(id(c) == id(a)) # False
Your issue:
In [1]: import pickle
In [2]: class C:
...: pass
...:
In [3]: hasattr(C,'foo')
Out[3]: False
In [4]: pickled = pickle.dumps(C)
In [5]: id(C)
Out[5]: 2887019170312
In [6]: C.foo = 'bar'
In [7]: id(C)
Out[7]: 2887019170312
In [8]: C_ = pickle.loads(pickled)
In [9]: id(C_)
Out[9]: 2887019170312
Solution. Make a copy
In [3]: C_ = type('C_', C.__bases__, dict(C.__dict__))
In [4]: pickled = pickle.dumps(C_)
In [5]: hasattr(C, 'foo')
Out[5]: False
In [6]: C.foo = 'bar'
In [7]: C_without_foo = pickle.loads(pickled)
In [8]: hasattr(C_without_foo, 'foo')
Out[8]: False
Upvotes: 0
Reputation: 429
I think the problem is that you are pickle-ing the class itself, as opposed to a specific object.
import pickle
class C:
pass
myC = C() #pickle myC object, not the C class
print(hasattr(myC, 'foo'))
pickled = pickle.dumps(myC)
myC.foo = 'bar'
print(hasattr(myC, 'foo'))
C_without_foo = pickle.loads(pickled)
print(hasattr(C_without_foo, 'foo'))
print(hasattr(myC, 'foo'))
Upvotes: 1