How is dill different from Python's pickle module?

橙三吉。 提交于 2020-05-17 08:20:16

问题


I have a large object in my Python3 code which, when tried to be pickled with the pickle module throws the following error:

TypeError: cannot serialize '_io.BufferedReader' object

However, dill.dump() and dill.load() are able to save and restore the object seamlessly.

  1. What causes the trouble for the pickle module?
  2. Now that dill saves and reconstructs the object without any error, is there any way to verify if the pickling and unpickling with dill went well?
  3. How's it possible that pickle fails, but dill succeeds?

回答1:


I'm the dill author.

1) Easiest thing to do is look at this file: https://github.com/uqfoundation/dill/blob/master/dill/_objects.py, it lists what pickle can serialize, and what dill can serialize.

2) you can try dill.copy and dill.check and dill.pickles to check different levels of pickling and unpickling. dill also more includes utilities for detecting and diagnosing serialization issues in dill.detect and dill.pointers.

3) dill is built on pickle, and augments it by registering new serialization functions.

4) dill includes serialization variants which enable the user to choose from different object dependency serialization strategies (in dill.settings) -- including source code extraction and object reconstitution with dill.source (and extension of the stdlib inspect module).



来源:https://stackoverflow.com/questions/58193119/how-is-dill-different-from-pythons-pickle-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!