00__00__00
00__00__00

Reputation: 5327

which python library to serialize a poorly designed class

Can anybody help me to understand how and why to choose among e.g. pickle and dill?

My use case is the following. I would like to dump an object which instance of a class derived by multiple inheritance from some external library classes. Moreover one attribute of the class is a dictionary, which has a function as a value. Unfortunately, that function is defined within the scope of the class.

def class:
    def f():
      def that_function():
        # do someth
      # back within f() scope
      self.mydata{'foo':that_function}

Any comment regard to robustness to external dependencies?

Or any other library I could consider for serialization?

Upvotes: 0

Views: 288

Answers (1)

Mike McKerns
Mike McKerns

Reputation: 35217

I'm the dill author. You should use pickle if all the objects you want to pickle can be pickled by pickle.dump. If one or more of the objects are unpicklable with pickle, then use dill. See the pickle docs for what can be pickled with pickle. dill can pickle most python objects, with some exceptions.

If you want to consider alternatives to dill, there's cloudpickle, which has a similar functionality to dill (and is very similar to dill when using dill.settings['recurse'] = True).

There are other serialization libraries, like json, but they actually serialize less objects than pickle does, so you'd not choose them to serialize a user-built class.

Upvotes: 1

Related Questions