Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!The term "object serialization" refers to process of converting state of an object into byte stream. Once created, this byte stream can further be stored in a file or transmitted via sockets etc. On the other hand, reconstructing the object from the byte stream is called deserialization.
Python's terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module available in Python's standard library provides functions for serialization (dump() and dumps()) and deserialization (load() and loads()).
The pickle module uses very Python specific data format. Hence, programs not written in Python may not be able to deserialize the encoded (pickled) data properly. Also it is not considered to be secure to unpickle data from un-authenticated source.
Pickle Protocols
Protocols are the conventions used in constructing and deconstructing Python objects to/from binary data. Currently pickle module defines 5 different protocols as listed below −
Sr.No. | Protocol & Description |
---|---|
1 |
Protocol version 0 Original "human-readable" protocol backwards compatible with earlier versions. |
2 |
Protocol version 1 Old binary format also compatible with earlier versions of Python. |
3 |
Protocol version 2 Introduced in Python 2.3 provides efficient pickling of new-style classes. |
4 |
Protocol version 3 Added in Python 3.0. recommended when compatibility with other Python 3 versions is required. |
5 |
Protocol version 4 Was added in Python 3.4. It adds support for very large objects. |
To know the highest and default protocol version of your Python installation, use the following constants defined in the pickle module −
The dump() and load() functions of the pickle module perform pickling and unpickling Python data. The dump() function writes pickled object to a file and load() function unpickles data from file to Python object.
dump() and load()
Following program pickle a dictionary object into a binary file.
When above code is executed, the dictionary object's byte representation will be stored in data.txt file.
To unpickle or deserialize data from a binary file back to dictionary, run following program.
Python console shows the dictionary object read from file.
{'age': 23, 'Gender': 'M', 'name': 'Ravi', 'marks': 75}
dumps() and loads()
The pickle module also consists of dumps() function that returns a string representation of pickled data.
Use loads() function to unpickle the string and obtain original dictionary object.
It will produce the following output −
{'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}
Pickler Class
The pickle module also defines Pickler and Unpickler classes. Pickler class writes pickle data to file. Unpickler class reads binary data from file and constructs Python object.
To write Python object's pickled data −
Unpickler Class
To read back data by unpickling binary file −
Objects of all Python standard data types are picklable. Moreover, objects of custom class can also be pickled and unpickled.
Python library also has marshal module that is used for internal serialization of Python objects.
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.