Unpickleable Data Types and dill
By now you should be starting to see that
pickle can handle many of the data types that you might run into with your Python scripts. Unfortunately, it can’t handle everything. Certain objects like database connections, network sockets, running threads, and so on can’t be serialized using
It can work with functions that yield results, nested functions, and quite a few other cases where
pickle doesn’t quite work on its own. To get started, I’ve created a new Python script called
You can see there was an issue here. When this tried to run, when
pickle tried to dump that lambda expression into a string, you had this result here where there’s a pickling error and it can’t pickle this
lambda function. So let’s try to get around this.
So now this case of
pickle can be replaced with
dill. And keep in mind that
dill still has the same API as
pickle, so you can continue to use the
.dumps() method, which will take this
square and serialize it into a byte string.
And look at that! You can see that it now printed out the byte string that represents that lambda.
dill can serialize quite a few advanced data types, so if you feel the need to save the state of something that
pickle isn’t working for, go ahead and try
There are still some cases that
dill can’t handle, so in the next lesson, you’re going to see how you can get around these strange cases by using the dunder methods
Become a Member to join the conversation.