top of page

Python Object Serialization and Deserialization with Pickle

Pickle module implements binary protocols for serializing and de-serializing a python object structure.


for example list, dictionary, method and class object can be serialized, or they can be restored.


Pickling is the process whereby a Python object hierarchy ic converted into a byte stream.

Unpickling is the inverse operation, whereby a byte stream(from a binary file) is converted back into an object hierarchy.


Pickling and unpickling also known as "serialization" and "deserialization"

To serialize an object, we can call pickle.dump() method, similarly to de-serialize a data stream , we have to call pickle.load() method.


The pickle module keeps track of the objects it has already serialized, so that later references to the same object won't be serialized again.



What can be pickle and unpickled?

The following types can be pickled.

  1. None, True, and False

  2. int, float, and complex types

  3. strings, bytes, and byte arrays

  4. tuples, lists, and dictionaries

  5. functions defined at the top level of module.

  6. built-in functions defined at the top level of module.

  7. classes that are defined at the top level of module.

Lambdas cannot pickled.

serializing and deserializing a dictionary object


serializing and deserializing a class object:


pickling and unpickling a list object:


pickling and unpickling a function:


 

Thanks for reading!!!

Your Rating and Review will be appreciated!!

Twitter: @LearnerLandmark

Recent Posts

See All
bottom of page