The pickle module in Python is used for serializing and de-serializing Python object structures, also known as marshaling or flattening. Serialization is the process of converting an object’s state to a byte stream, and the opposite operation, extracting the object from the byte stream, is called de-serialization.
- Importing the module: Like any other Python module, before using
pickle
, you first need to import it.
import pickle
- Pickling: The process of serialization is called pickling. The
pickle.dump()
function is used to serialize an object hierarchy. It takes two arguments: the object you want to pickle and the file object in which you want to store the pickled object.
# create an example dictionary data = {"cat": "meow", "dog": "bark"} # open a file to write binary with open("pets.pkl", "wb") as file: pickle.dump(data, file)
In the above code, we created a dictionary and pickled it into a file called “pets.pkl”. The “wb” mode stands for “write binary”.
- Unpickling: The process of deserialization is called unpickling. The
pickle.load()
function is used to unpickle the object.
# open the file in read binary mode with open("pets.pkl", "rb") as file: loaded_data = pickle.load(file) print(loaded_data)
When you run the above code, it will display the original dictionary. The “rb” stands for “read binary”.
- pickle.dumps() and pickle.loads(): These functions are used to pickle and unpickle data, but instead of writing or reading from a file, they simply return the pickled data as a bytes object or load from a bytes object.
# Pickling to a bytes object pickled_data = pickle.dumps(data) print(pickled_data) # This will print a bytes object # Unpickling from a bytes object unpickled_data = pickle.loads(pickled_data) print(unpickled_data) # This will print the original dictionary
Some important points:
- The
pickle
module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source. - Not all Python data types can be pickled. Functions, classes, and methods cannot be pickled, so you cannot pickle the entire state of your Python program.
Despite these limitations, pickle
is very useful for storing and retrieving complex data in Python.