Need to keep a Python object after your program stops, without rerunning an expensive processing step? Pickling converts an object into binary data that you can store and later reconstruct. Python’s built-in pickle module handles forms such as tuples, dictionaries, and instances of custom classes. section 11.3 section 11.3.1
import pickle #1
task_tuple = (1001, "Homework", 5)
task_dict = {'task_id': '1002', 'title': 'Laundry', 'urgency': 3}
with open("task_tuple_saved.pickle", "wb") as file:
pickle.dump(task_tuple, file)
with open("task_dict_saved.pickle", "wb") as file:
pickle.dump(task_dict, file)
This listing saves a tuple and a dictionary to separate files. Open each file with "wb" for binary writing, then pass the object and file to pickle.dump. The resulting .pickle files are not meant to be read in a text editor. section 11.3.1
Restore and check a saved object
Open the saved file with "rb", then assign the result of pickle.load(file) to a variable. For example, after loading the saved tuple as task_tuple_loaded, `assert task_tuple == task_tuple_loaded` checks that its value was preserved. dump writes the object; load returns the reconstructed object. section 11.3.2
For binary data held in memory rather than a file, use pickle.dumps to produce bytes and pickle.loads to reconstruct an object from those bytes. The file-based counterparts are dump and load. section 11.3.2 section 11.3.3
class Task:
def __init__(self, title, urgency):
self.title = title
self.urgency = urgency
task = Task("Laundry", 3)
with open("task_class_saved.pickle", "wb") as file:
pickle.dump(task, file)
with open("task_class_saved.pickle", "rb") as file:
task_class_loaded = pickle.load(file)
assert task.__dict__ == task_class_loaded.__dict__
assert task is not task_class_loaded
A custom Task instance can be saved and restored without special encoding instructions. Its restored attributes match the original’s, but it is a different object. Keep the class definition available when loading: without Task in the namespace, loading fails. section 11.3.2
Choose pickle when you need to preserve Python objects that JSON does not handle natively, such as custom instances or functions. It does not handle every object: modules, open files, and database connections are examples of limits. For data-related projects, it can also read and write faster than CSV while using less storage. section 11.3.3 section 11.6
Treat trust as a requirement, not an afterthought. A crafted object can arrange for a command to run when its pickle is loaded; custom pickling behavior can also have effects while saving. Do not pickle or unpickle data from potentially untrusted sources. section 11.3.3 section 11.6


