使用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
我不确定代码是否可以修改内置函数...
这听起来很神秘。
答案 0 :(得分:1)
我是dill
作者。不同的设置(例如byref
)用于修改dump
中包含的对象类型。 pickle
,因此dill
使用递归对象序列化,这意味着当对象引用另一个对象时,它可能(或可能不)基于序列化设置对其进行序列化。 pickle
通常通过引用(byref=True
)序列化函数和类,这意味着它们仅在导入时按名称序列化。 dill
有几个设置,例如byref=False
(它序列化函数和类对象而不是引用)。还有recurse
,它处理全局引用的序列化方式 - recurse=False
将所有全局变量序列化为dict,而recurse=True
仅序列化全局变量中的引用项和这些项的项参考,等等。
简而言之,无论你序列化的是什么都有一些全局引用,它会触发递归序列化,直到它遇到dill
无法发现的东西。
以下是您可以尝试的一些事项:
recurse=True
)更改哪些对象dill
尝试使用目标对象进行序列化。您可以使用dill.settings
全局设置设置。pickle
如何序列化您的班级。由于您没有发布演示错误的实际示例,因此很难深入回答。