Python Pickle Tutorial - Serialize and De-serialize Objects


Python Pickle

The Python Pickle module is used to serialize and de-serialize Python objects. Serialization refers to converting a Python object into a stream of characters that can be written to a file, while de-serialization reconstructs the object from this serialized data stored in a file.

Among the Python community, the term Pickle is often used in various contexts, such as Pickling, Picklable, Pickled, and Unpickle. These terms will become familiar as you work with the Python Pickle module.


Which Datatypes can be Pickled?

Following Python datatypes can be pickled:

  • Booleans - True, False
  • Integers - 25, 6, 896
  • Floats - 2.56, 8.124, 2.14
  • Complex Numbers - 3+4j, 9-j, 7j
  • Strings - (Normal, Unicode)
  • Tuples
  • Lists
  • Sets
  • Dictionaries
  • Top-level Functions and Class Objects of a Module

Which Datatypes Cannot be Pickled?

Some Python datatypes and objects cannot be pickled:

  • Generators
  • Inner classes
  • Lambda functions
  • Defaultdicts

Importing the Pickle Module

To use the Pickle module in your program, import it as follows:

import pickle

Examples

1. Pickling a Dictionary and Writing it to a File

In the following example, we pickle a dictionary and write its serialized data to a file. The file does not need a specific extension for pickle files.

Python Program

import pickle

# Dictionary to be pickled
marks = { 'Alex': 87, 'Lini': 92, 'Kiku': 90 }

# Open a file in write-binary mode
picklefile = open('marks', 'wb')

# Pickle the dictionary and write it to the file
pickle.dump(marks, picklefile)

# Close the file
picklefile.close()

Explanation:

  1. The dictionary marks is pickled using the pickle.dump() function and written to the file marks.
  2. We open the file in write-binary mode using open('marks', 'wb') to ensure we can store the pickled data.
  3. After writing the serialized data to the file, the file is closed to ensure data integrity.

The file marks is now created in the current working directory.


2. Un-pickling or De-serializing Data

In this example, we unpickle the file created in the previous example to retrieve the original dictionary.

Python Program

import pickle

# Open the pickle file in read-binary mode
picklefile = open('marks', 'rb')

# Unpickle the data from the file
marks = pickle.load(picklefile)

# Close the file
picklefile.close()

# Print the unpickled dictionary
print(marks)
print(type(marks))

Explanation:

  1. The file marks is opened in read-binary mode using open('marks', 'rb').
  2. The pickle.load() function is used to read and unpickle the data from the file, reconstructing the original dictionary.
  3. The type of the unpickled object is verified using print(type(marks)), confirming that the object is still a dictionary.

Output

{'Alex': 87, 'Lini': 92, 'Kiku': 90}

3. Pickling and Unpickling a Custom Class Object

In this example, we pickle and unpickle an instance of a custom class. This demonstrates how Python objects of user-defined classes can also be serialized and deserialized.

Python Program

import pickle

# Custom class definition
class Student:
	def __init__(self, name, grade):
		self.name = name
		self.grade = grade

# Create an object of the Student class
student1 = Student('John', 'A')

# Pickle the object
with open('student1.pkl', 'wb') as f:
	pickle.dump(student1, f)

# Unpickle the object
with open('student1.pkl', 'rb') as f:
	unpickled_student = pickle.load(f)

# Print the unpickled object details
print(unpickled_student.name, unpickled_student.grade)

Explanation:

  1. A custom class Student is defined with attributes name and grade.
  2. An object student1 is created and pickled to a file named student1.pkl.
  3. The pickled object is then unpickled, and the attributes of the unpickled object are printed.

Output

John A

Additional Reading

If you found this tutorial helpful, check out our other tutorials on pickling:


Summary

In this tutorial, we covered how to use the Python Pickle module to serialize and deserialize Python objects. We demonstrated how to pickle dictionaries, custom class objects, and how to unpickle and reconstruct the original objects. The pickle.dump() and pickle.load() methods are essential for saving and loading Python objects in a serialized format.




Python Libraries