跑步:
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)
导致此异常:
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)
如果我删除它,它会起作用,pickle.HIGHEST_PROTOCOL
但如果可能的话,我宁愿使用新协议。
我只是没有得到泡菜协议描述。谁能帮我吗?一定有什么神奇的方法……