如何使一个自定义异常类与多个init args可选 [英] How to make a custom exception class with multiple init args pickleable
问题描述
为什么我的自定义Exception类不能使用pickle模块来正确序列化/取消排序?
import pickle
/ pre>
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)
此代码引发以下错误:
追溯(最近的电话最后):
文件test.py,第13行,< module>
obj = pickle.loads(str)
文件/usr/lib/python2.7/pickle.py,第1382行,加载
返回Unpickler(文件).load()
文件/usr/lib/python2.7/pickle.py,第858行,加载
dispatch [key](self)
文件/usr/lib/python2.7 /pickle.py,第1133行,load_reduce
value = func(* args)
TypeError:__init __()只需要3个参数(2个给定)
我确定这个问题源自于我如何使一个类别酸洗的方式缺乏知识。有趣的是,当我的类不扩展异常时,不会发生此问题。
感谢任何帮助。
凯尔
编辑:修正我的电话超级每个shx2
编辑:清理标题/内容解决方案使
arg2
可选:class MyException(Exception):
def __init __(self,arg1,arg2 = None):
self.arg1 = arg1
self.arg2 = arg2
super(MyException,self).__ init __(arg1)
基础
异常
class定义了一个.__ reduce__ ()
方法使扩展(基于C)的类型可选,该方法仅期望一个参数(即.args
);请参阅BaseException_reduce()
函数在C源。
最简单的解决方法是使额外的参数可选。
__ reduce __
方法还还包括.args
和<$ c $之外的任何其他对象属性c> .message ,您的实例正确重新创建:>>> e = MyException('foo','bar')
>>> e .__ reduce __()
(< class'__main __。MyException'>('foo',),{'arg1':'foo','arg2':'bar'})
>>> pickle.loads(pickle.dumps(e))
MyException('foo',)
>>> e2 = pickle.loads(pickle.dumps(e))
>>> e2.arg1
'foo'
>>> e2.arg2
'bar'
Why does my custom Exception class below not serialize/unserialize correctly using the pickle module?
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)
This code throws the following error:
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)
I'm sure this problem stems from a lack of knowledge on my part of how to make a class pickle-friendly. Interestingly, this problem doesn't occur when my class doesn't extend Exception.
Thanks for any help. Kyle
EDIT: Fixing my call to super per shx2 EDIT: Cleaning up title/content
解决方案Make
arg2
optional:class MyException(Exception): def __init__(self, arg1, arg2=None): self.arg1 = arg1 self.arg2 = arg2 super(MyException, self).__init__(arg1)
The base
Exception
class defines a.__reduce__()
method to make the extension (C-based) type picklable and that method only expects one argument (which is.args
); see theBaseException_reduce()
function in the C source.The easiest work-around is making extra arguments optional. The
__reduce__
method also includes any additional object attributes beyond.args
and.message
and your instances are recreated properly:>>> e = MyException('foo', 'bar') >>> e.__reduce__() (<class '__main__.MyException'>, ('foo',), {'arg1': 'foo', 'arg2': 'bar'}) >>> pickle.loads(pickle.dumps(e)) MyException('foo',) >>> e2 = pickle.loads(pickle.dumps(e)) >>> e2.arg1 'foo' >>> e2.arg2 'bar'
这篇关于如何使一个自定义异常类与多个init args可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!