The Python Pickle module is used to perform serialization and deserialization of Python objects
Serializing a Python object means converting it into a byte stream that can be stored into a file or into a string
Pickle data can then be read using the process called deserialization. To store a Pickled object into a string, use the dumpS() function
To read an object from a string that contains its Pickled representation, you can use the loadS() function
Let's see an example on how you can use the Pickle module to serialize a Python list
After importing the Pickle module, we define a list and then use the Pickle dumpS() function to generate a byte representation of our list
Then, we store the Pickled string into a variable and use the loadS() function to convert the byte string back to our original list
The letter S at the end of the dumpS() and loadS() Pickle functions stands for string
The Pickle module also provides two functions that use files to store and read Pickle data
The names of the two functions are dump() and load(). With the Pickle module, you can save different types of Python objects
Let's use the dumpS() function to Pickle a Python dictionary. Then, we can use the loadS() function to get a dictionary back from its Pickled representation
You now know that we can also save dictionary objects into a string of bytes using Pickle
The Pickle module also allows to store the Pickled representation of a Python object to a file
To store a Pickled object to a file, use the dump() function instead of the dumpS() function
To read an object from its Pickled representation stored in a file, use the load() function
First of all, we will open a file in binary mode using the Python open() function
Then, store a Pickled representation of a dictionary in the file and then we will close the file
The data.pickled file will be created in the same directory as your Python program
Also, remember to close the file when you are done with it
Now, let's look at the content of the file. You will see that data is in binary format
Then, read the bytes from the file and get back the original dictionary object using the load() function
This time, we have opened a file in read binary mode, considering that we only want to read its content
The next thing I want to do in this tutorial is to see if the Pickle module can also be used to see the layout of a dictionary
This means that we can use Pickled to serialize nested objects. Let's find out if a Python nested dictionary can be serialized and deserialized using the Pickled module
Update the dictionary used in the previous example to include dictionaries as values marked to each key
Then, write the Pickled nested dictionary for the file. To do that, we will use code that is identical to the one we used in the previous example
to make a representation of a basic dictionary. Now, convert the Pickled data back to the nested dictionary and confirm that the format of the dictionary is correct
As you can see, the nested dictionary looks good
Now, I want to find out if I can Pickle a Python custom class
Let's create a class called Animal that contains two attributes, name and group
Then, create one object and Pickle it into a file
Then, read the data using the PickleLoad function. As you can see, the attributes of our objects are the ones we have used in the definition of our Python class
It's time to find out if we can save multiple objects using Pickle
Using the same class defined in the previous example, we will save two objects into a file using the Pickle module
Let's create two objects of type Animal and Pickle them into a file as a list of objects
Then, let's use the following code to access each object using a for loop
Until now, we had to close file objects as soon as we didn't need them anymore
What other options we have? Instead of doing that, you can use the withOpenStatement that takes care of closing the file automatically
Let's see how our code to write multiple objects changes when we use the withOpenStatement
And now, let's use the withOpenStatement also to read the Pickle data
As you can see, we don't have to call anymore the close function on the file handler every time we read or write a file
So far, we've used the Pickle module with variables
But I wonder what happens if I try to serialize a lambda function
Let's define a simple lambda function that returns the sum of two numbers
And then, try to use the Pickle.downtest function to serialize the function
As you can see, we get an error back. This means that the Pickle module doesn't allow to serialize a lambda function
As an alternative, you can use the dill module that extends the functionality of the Pickle module
If you don't have the dill module installed, use the command pip install dill to install it
Now, import the dill module and generate a serialized version of the lambda function using the dill.downtest function
From Pickle.data, we will use the loadest function of the dill module
It works fine
When we deserialize our lambda function, we get back the function we expect
If you want to know more about other specific use cases of the Pickle module, have a look at the article mentioned in the description below
If you enjoyed this tutorial, give it a like. Also, don't forget to subscribe to CodeSlider so we can be in touch and we can keep learning Python together
See you in the next video.