Object Serialization with Pickle

Objects can be serialized: stored in a format for later use. Once an object is serialized, it can be stored in a file and loaded from a file.

The pickle module can be used for object serialization.

Related course:
Python Beyond the Basics – Object-Oriented Programming

Create and dump an object

We start by creating a class and object. Then we use the module dumps() with as parameter the object. We can simply print the serialized output, because why not?

Store and load object from file

More practical is to store the serialized object in file. Then we can reload it from that file at any time. To store a serialized object into a file:

Then the object can be loaded from the file. This can be done at any time or even from another program.

Previous Post
Next Post

Leave a Reply