为什么我下面的自定义异常类没有使用 pickle 模块正确序列化/反序列化?
import pickle
class MyException(Exception):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
super(MyException, self).__init__(arg1)
e = MyException("foo", "bar")
str = pickle.dumps(e)
obj = pickle.loads(str)
此代码引发以下错误:
Traceback (most recent call last):
File "test.py", line 13, in <module>
obj = pickle.loads(str)
File "/usr/lib/python2.7/pickle.py", line 1382, in loads
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 1133, in load_reduce
value = func(*args)
TypeError: __init__() takes exactly 3 arguments (2 given)
我确信这个问题源于我对如何使课程对泡菜友好的知识缺乏。有趣的是,当我的类没有扩展 Exception 时,不会出现这个问题。
谢谢你的帮助。凯尔
编辑:修复我对超级每个 shx2 的调用编辑:清理标题/内容