内置功能的莳萝/泡菜问题

时间:2016-11-22 00:14:27

标签: python pickle dill

使用dill序列化类时:

import dill, pickle

class project(object):
    def __init__(self, name='', folder='', user_id='', version=-1 ):
        self.name, self.folder, self.user_id, self.version = name, folder, user_id, version
# Other code, not shown, since Big class .......

dill.dump(proj1, open('test_dill.pkl', 'w'), byref=False )

我收到此消息:

PicklingError: Can't pickle <built-in function raw_input>: it's not the same object as __builtin__.raw_input


  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 236, in dump
    pik.dump(obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 224, in dump
    self.save(obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 606, in save_list
    self._batch_appends(iter(obj))

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 642, in _batch_appends
    save(tmp[0])

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 993, in save_builtin_method
    StockPickler.save_global(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 759, in save_global
    (obj, module, name))

PicklingError: Can't pickle <built-in function raw_input>: it's not the same object as __builtin__.raw_input

我不确定代码是否可以修改内置函数...
这听起来很神秘。

1 个答案:

答案 0 :(得分:1)

我是dill作者。不同的设置(例如byref)用于修改dump中包含的对象类型。 pickle,因此dill使用递归对象序列化,这意味着当对象引用另一个对象时,它可能(或可能不)基于序列化设置对其进行序列化。 pickle通常通过引用(byref=True)序列化函数和类,这意味着它们仅在导入时按名称序列化。 dill有几个设置,例如byref=False(它序列化函数和类对象而不是引用)。还有recurse,它处理全局引用的序列化方式 - recurse=False将所有全局变量序列化为dict,而recurse=True仅序列化全局变量中的引用项和这些项的项参考,等等。

简而言之,无论你序列化的是什么都有一些全局引用,它会触发递归序列化,直到它遇到dill无法发现的东西。

以下是您可以尝试的一些事项:

  1. 将文件中的对象分隔成单独的文件 - 这样您就可以通过引用更多地依赖于酸洗。
  2. 使用不同的设置(例如recurse=True)更改哪些对象dill尝试使用目标对象进行序列化。您可以使用dill.settings全局设置设置。
  3. 向您的班级添加序列化助手方法,通知pickle如何序列化您的班级。
  4. 重构你的课程(不知何故,因为我无法看到它的内容)以避免序列化问题。
  5. 由于您没有发布演示错误的实际示例,因此很难深入回答。