May 2021
Intermediate to advanced
142 pages
3h 37m
English
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 ...