为什么我的自定义异常unpickle失败

时间:2017-01-20 18:19:18

标签: python python-2.7 python-3.x

import pickle

class ABError(Exception):
    def __init__(self, a, b):
        super(ABError, self).__init__(a)
        self.a = a
        self.b = b

class ABCDError(ABError):
    def __init__(self, a, b, c, d):
        super(ABCDError, self).__init__(a, b)
        self.c = c
        self.d = d

err = ABCDError("aaaaa", "bbbbb", "ccccc", "ddddd")

pickled_err = pickle.dumps(err)

original_err = pickle.loads(pickled_err)  # Fails

我得到以下追溯:

Traceback (most recent call last):
  File "pickle_pain.py", line 19, in <module>
    original_err = pickle.loads(pickled_err)  # Fails
  File "/usr/lib/python2.7/pickle.py", line 1388, in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1139, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 5 arguments (2 given)

我已经做了一些谷歌搜索,但努力找到一个很好的答案,为什么会发生这种情况,而不是绝望开始学习泡菜序列化格式,以便逐步完成: - /

编辑:python3中的行为是相同的,虽然错误消息稍微好一些:

Traceback (most recent call last):
  File "pickle_pain.py", line 19, in <module>
    original_err = pickle.loads(pickled_err)  # Fails
TypeError: __init__() missing 3 required positional arguments: 'b', 'c', and 'd'

0 个答案:

没有答案