Skip to Content
Intuitive Python
book

Intuitive Python

by David Muller
May 2021
Intermediate to advanced
142 pages
3h 37m
English
Pragmatic Bookshelf
Content preview from Intuitive Python

Serializing Python Objects with the pickle Module

Python only includes one standard library module named after a food, and that module is named pickle. The pickle module allows arbitrary Python objects to be serialized into a stream of bytes. This stream of bytes can then be unpickled by another Python program to restore the object that was originally pickled. Let’s explore this in a little more detail.

In the following example pickle is used to serialize and de-serialize a list containing strings and None:

 import​ ​pickle
 
 l = [​"a"​, ​"b"​, None, ​"c"​]
 
 dumped_l = pickle.dumps(l)
 print​(dumped_l)
 
 loaded_l = pickle.loads(dumped_l)
 print​(loaded_l)

If you run python3 pickle_example.py, you should see output like ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pythonic Programming

Pythonic Programming

Dmitry Zinoviev
Python for Geeks

Python for Geeks

Muhammad Asif

Publisher Resources

ISBN: 9781680508635Errata Page