pickle/unpickle class which has changed

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 6 08:55:12 EST 2012


On Tue, 06 Mar 2012 07:34:34 -0500, Neal Becker wrote:

> What happens if I pickle a class, and later unpickle it where the class
> now has added some new attributes?

Why don't you try it?

py> import pickle
py> class C:
...     a = 23
...
py> c = C()
py> pickled = pickle.dumps(c)
py> C.b = 42  # add a new class attribute
py> d = pickle.loads(pickled)
py> d.a
23
py> d.b
42


Unless you mean something different from this, adding attributes to the 
class is perfectly fine.

But... why are you dynamically adding attributes to the class? Isn't that 
rather unusual?


-- 
Steven



More information about the Python-list mailing list