Understanding Pickle Module in Python

Praffulla Dubey
2 min readJan 6, 2021

--

Source: https://www.smartfile.com/blog/python-pickle-security-problems-and-solutions/

Pickle module comes to our mind when we talk about “serialization” and “deserialization” in Python. So basically we use pickle module when a python object structure needs to be serialized or de-serialized.

We can save any of the python objects (like list, dictionary, or any trained model for prediction) on disk by pickling that object.

Before writing the object to a file, we serialize the object first by using pickle module.

We convert the python objects into character stream and this character stream contains all the necessary information required to recreate the python object into another python script. As a first step we need to import the pickle module to use it.

Pickle module has basically two functions i.e. pickle.dump() and pickle.load().

We can save any python object as pickle file by using following code:

Use of dump()

“with open” is a smart way of pickling as it automatically closes the file. In the above code “wb” stands for write binary i.e. the file will have the data in binary form. “pc” is a temporary variable. The pickle file is saved by .pkl extension.

We can load any pickle file into python object using following code:

Use of load()

In the above code “rb” stands for read binary. The “pickle.load()” function is used for unpickling the pickled object. It converts the pickled data into its original form i.e. python object.

There is another method to dump and load the pickle file is shown in the figure below, but it is not safe as we need to close the file if we use this method.

Another method for dump() and load()

Therefore, pickle module is an important module and we should only serialize or de-serialize only objects from trusted sources.

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to connect with me on LinkedIn.

--

--

Praffulla Dubey
Praffulla Dubey

Written by Praffulla Dubey

Data Science and Python enthusiastic.

No responses yet