Duration:
import pickle class Foo(str): def __new__(cls, s, bar): self = super(Foo, cls).__new__(cls, s) self.bar = bar return self with open('foo.pkl', 'wb') as w: x = pickle.dump(Foo('', 1), w, pickle.HIGHEST_PROTOCOL) with open('foo.pkl', 'rb') as w: x = pickle.load(w)
Results in this exception:
Traceback (most recent call last): File "pkl.py", line 13, in <module> x = pickle.load(w) File "/usr/lib/python2.7/pickle.py", line 1378, in load return Unpickler(file).load() File "/usr/lib/python2.7/pickle.py", line 858, in load dispatch[key](self) File "/usr/lib/python2.7/pickle.py", line 1083, in load_newobj obj = cls.__new__(cls, *args) TypeError: __new__() takes exactly 3 arguments (2 given)
It works if I remove pickle.HIGHEST_PROTOCOL , but I would prefer to use the new protocol if possible.
I just don't get the pickle protocol description. Can someone help me? There must be some way with magic methods ...
source share