Python: strle-generated str object

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 ...

+4
source share
1 answer

This works without HIGHEST_PROTOCOL because older protocols do not use the __new__ constructor for unpainted objects.

But the main thing is that you use str serialization methods while you add a new argument to __new__ . The pickle module or built-in str type is aware of this and this will result in an error.

To fix this, you must define the __getnewargs__ method and return (self.__class__, self.bar) , which should be sufficient to reuse it.

Documentation of the special methods used by the pickle module can be found here.

+5
source

All Articles