【问题标题】:Why can't dill/pickle class definition?为什么不能 dill/pickle 类定义?
【发布时间】:2015-02-16 13:46:04
【问题描述】:

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

在尝试序列化类定义时,我不断从dill 获得AssertionError。我想知道为什么其中一个有效而另一个失败:

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。

【问题讨论】:

  • 你读过例如stackoverflow.com/questions/2626636/pickling-a-class-definition?此外,您的 super 调用似乎不正确 - 应该是 super(MyClassInit, self).__init__()。
  • 可以执行,没有抛出AssertionError。
  • 感谢 jonrsharpe!问题只是缺少, self。更正 dumps() 后,就像在链接的帖子中一样,按预期工作。
  • @SmCaterpillar,很有趣!你使用的是同一个 Python 和 dill 版本吗?
  • 嗯,我使用了 python 2.7.9,但在 IPython Notebook 环境中。还是那不应该改变什么?!

标签: python pickle dill


【解决方案1】:

我是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 我不知道……这可能更可取。

【讨论】:

  • 谢谢马克。感谢您发布问题/错误报告等。
猜你喜欢
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多