The pickle link states that the set of objects that can be pickled is rather limited. In fact, I have a function that returns a dynamically generated class, and I found that I can not saw through instances of this class:
>>> import pickle >>> def f(): ... class A: pass ... return A ... >>> LocalA = f() >>> la = LocalA() >>> with open('testing.pickle', 'wb') as f: ... pickle.dump(la, f, pickle.HIGHEST_PROTOCOL) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: Can't pickle local object 'f.<locals>.A'
Such objects are too complicated for pickle . OK. Now, what is magic, if I try to rekindle a similar object, but a derived class, it works!
>>> class DerivedA(LocalA): pass ... >>> da = DerivedA() >>> with open('testing.pickle', 'wb') as f: ... pickle.dump(da, f, pickle.HIGHEST_PROTOCOL) ... >>>
What's going on here? If this is so simple, why does pickle not use this workaround to implement the dump method, which allows pickling "local objects"?
fonini
source share