2

dill是腌制大多数 Python 对象的好工具,我在 IPython 中并行使用它来序列化计算。我一直在讨论的一个问题是围绕类定义进行挖掘。我得到的错误之一解释如下。

在尝试序列化类定义时,我不断AssertionErrordill. 我想知道为什么其中一个有效而另一个失败:

class MyClassEmpty(object):
    pass

class MyClassInit(object):
    def __init__(self):
        super(MyClassInit).__init__()

dill.dumps(MyClassEmpty)  # returns: '\x80\x02cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01U\x08TypeTypeq\x02\x85q\x03Rq\x04U\x0cMyClassEmptyq\x05h\x01U\nObjectTypeq\x06\x85q\x07Rq\x08\x85q\t}q\n(U\n__module__q\x0bU\x08__main__q\x0cU\x07__doc__q\rNutq\x0eRq\x0f.'

dill.dumps(MyClassInit)  # AssertionError at line 244 of MyClassEmpty (assert id(obj) not in self.memo)

我在 Python 2.7.6 上使用 dill 0.2.2。

4

1 回答 1

6

我是 的作者dill。该super问题应该得到解决——参见:https ://github.com/uqfoundation/dill/issues/26

>>> class MyClassEmpty(object):
...   pass
... 
>>> class MyClassInit(object):
...   def __init__(self):
...     super(MyClassInit).__init__()
... 
>>> import dill
>>> 
>>> dill.dumps(MyClassEmpty)
'\x80\x02cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01U\x08TypeTypeq\x02\x85q\x03Rq\x04U\x0cMyClassEmptyq\x05h\x01U\nObjectTypeq\x06\x85q\x07Rq\x08\x85q\t}q\n(U\n__module__q\x0bU\x08__main__q\x0cU\x07__doc__q\rNutq\x0eRq\x0f.'
>>> dill.dumps(MyClassInit) 
'\x80\x02cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01U\x08TypeTypeq\x02\x85q\x03Rq\x04U\x0bMyClassInitq\x05h\x01U\nObjectTypeq\x06\x85q\x07Rq\x08\x85q\t}q\n(U\n__module__q\x0bU\x08__main__q\x0cU\x07__doc__q\rNU\x08__init__q\x0ecdill.dill\n_create_function\nq\x0f(cdill.dill\n_unmarshal\nq\x10U\x90c\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00C\x00\x00\x00s\x14\x00\x00\x00t\x00\x00t\x01\x00\x83\x01\x00j\x02\x00\x83\x00\x00\x01d\x00\x00S(\x01\x00\x00\x00N(\x03\x00\x00\x00t\x05\x00\x00\x00supert\x0b\x00\x00\x00MyClassInitt\x08\x00\x00\x00__init__(\x01\x00\x00\x00t\x04\x00\x00\x00self(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<stdin>R\x02\x00\x00\x00\x02\x00\x00\x00s\x02\x00\x00\x00\x00\x01q\x11\x85q\x12Rq\x13c__builtin__\n__main__\nh\x0eNN}q\x14tq\x15Rq\x16utq\x17Rq\x18.'
>>> 
>>> class MyClassInit2(object):
...   def __init__(self):
...     super(MyClassInit, self).__init__()
... 
>>> dill.dumps(MyClassInit2)
'\x80\x02cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01U\x08TypeTypeq\x02\x85q\x03Rq\x04U\x0cMyClassInit2q\x05h\x01U\nObjectTypeq\x06\x85q\x07Rq\x08\x85q\t}q\n(U\n__module__q\x0bU\x08__main__q\x0cU\x07__doc__q\rNU\x08__init__q\x0ecdill.dill\n_create_function\nq\x0f(cdill.dill\n_unmarshal\nq\x10U\x93c\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00C\x00\x00\x00s\x17\x00\x00\x00t\x00\x00t\x01\x00|\x00\x00\x83\x02\x00j\x02\x00\x83\x00\x00\x01d\x00\x00S(\x01\x00\x00\x00N(\x03\x00\x00\x00t\x05\x00\x00\x00supert\x0b\x00\x00\x00MyClassInitt\x08\x00\x00\x00__init__(\x01\x00\x00\x00t\x04\x00\x00\x00self(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<stdin>R\x02\x00\x00\x00\x02\x00\x00\x00s\x02\x00\x00\x00\x00\x01q\x11\x85q\x12Rq\x13c__builtin__\n__main__\nh\x0eNN}q\x14tq\x15Rq\x16utq\x17Rq\x18.'

顺便说一句:dill对于pickle某些涉及类的情况(主要),并pickle抛出一个AssertionError作为它可能引发的 3-4 个错误之一。为什么不只是一个PicklingError我不知道……这可能更可取。

于 2015-02-16T14:15:07.587 回答