为什么不能莳萝/腌制类定义? [英] Why can't dill/pickle class definition?

查看:100
本文介绍了为什么不能莳萝/腌制类定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dill是用于腌制大多数Python对象的好工具,我在IPython中并行使用它来序列化计算.我一直讨论的一个问题是关于莳萝类定义.以下是我得到的错误之一.

dill is a great tool for pickling most the Python objects, I use it in IPython parallel to serialize calculations. One issue I keep getting into is around dill-ing class definitions. One of the errors I get is explained below.

在尝试序列化类定义时,我一直从dill获取AssertionError.我不知道为什么其中一个有效而另一个无效:

While trying to serialize class definitions, I keep getting AssertionError from dill. I wonder why one of these works and the other fails:

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)

我正在使用莳萝0.2.2.的Python 2.7.6.

I'm on Python 2.7.6 using dill 0.2.2.

推荐答案

我是dill的作者. super问题应得到解决-请参阅: https://github.com/uqfoundation/dill /issues/26

I'm the author of dill. The super issue should be resolved -- see: 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.'

BTW:在某些涉及类的情况下(主要),dill会向pickle倾斜,并且pickle会抛出AssertionError作为它可能引起的3-4个错误之一.为什么不只是一个我不知道的PicklingError……可能更可取.

BTW: dill punts to pickle for certain cases involving classes (primarily), and pickle throws an AssertionError as one of the 3-4 errors that it might evoke. Why there's not just a PicklingError I don't know… that might be more preferable.

这篇关于为什么不能莳萝/腌制类定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆