
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python object serialization
Serialization is a process in which an object is transformed into a format that can be stored/save (in a file or memory buffer), so we are able to deserialize it later and recover the original content/object from the serialized format. We are going to use a python pickle module to do all these operations.
What is pickling?
Python pickle module is used for serializing and de-serializing python object structures. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshaling. We can convert the byte stream (generated through pickling) back into python objects by a process called as unpickling.
To pickle an object we just need to −
- Import the pickle
- Call the dumps() function
import pickle class Vehicle: def __init__(self, number_of_doors, color): self.number_of_doors = number_of_doors self.color = color class Car(Vehicle): def __init__(self, color): Vehicle.__init__(self, 5, color) Maruti = Car('Red') print(str.format('My Vehicle Maruti is {0} and has {1} doors', Maruti.color, Maruti.number_of_doors)) pickle_Maruti = pickle.dumps(Maruti) print('Here is my pickled Vehicle: ') print(pickle_Maruti)
Output
My Vehicle Maruti is Red and has 5 doors Here is my pickled Vehicle: b'\x80\x03c__main__\nCar\nq\x00)\x81q\x01}q\x02(X\x0f\x00\x00\x00number_of_doorsq\x03K\x05X\x05\x00\x00\x00colorq\x04X\x03\x00\x00\x00Redq\x05ub.'
In the above example, we have created an instance of a Car class and then we’ve pickled it, transforming our car instance into a simple array of bytes. After our car instance is pickled, we can easily store it on a binary file or in a db field and restore it later to transform back this bunch of bytes in an object hierarchy.
Note: In case we want to create a file with a pickled object, we need to use the dump() method instead of the dumps() method.
Unpickling
It is the inverse (or pickling) operation, where we take the binary stream and convert it into an object hierarchy.
The unpickling is done using the load() function of the pickle module and returns back a complete object hierarchy from the byte stream.
Below is the load
import pickle class Vehicle: def __init__(self, number_of_doors, color): self.number_of_doors = number_of_doors self.color = color class Car(Vehicle): def __init__(self, color): Vehicle.__init__(self, 5, color) Maruti = Car('Red') print(str.format('My Vehicle Maruti is {0} and has {1} doors', Maruti.color, Maruti.number_of_doors)) pickle_Maruti = pickle.dumps(Maruti) #Now, let's unpickle our car Maruti creating another instance, another car ... unpickle_Maruti Hyundai = pickle.loads(pickle_Maruti) #Set another color of our new instance Hyundai.color = 'Black' print(str.format("Hyundai is {0} ", Hyundai.color)) print(str.format("Maruti is {0} ", Maruti.color))
In above example, you can see that we have pickled our first car object (Maruti) and then we have unpickled it to another variable (Hyundai) and so we have in a sense – cloned Maruti to create Hyundai.
Output
My Vehicle Maruti is Red and has 5 doors Hyundai is Black Maruti is Red
Pickle vs JSON
JSON stands for Javascript Object Notation, is a lightweight format for data-interchange and is humans readable. One of the big advantages of JSON over pickle is that it’s standardized and language-independent. It is much more secure and faster than pickle.
One more alternative to pickle is cPickle, which is very much like a pickle but is written in C language and is 1000 times faster. You can use the same files for pickle and cPickle.
import json mylist = [2, 4, 5, "ab", "cd", "ef"] print("Here is my list: ", mylist) json_string = json.dumps(mylist ) print("Here is my json encoded object: ", json_string) print ("Here is JSON back to a data structure: ",json.loads(json_string))
Output
Here is my list: [2, 4, 5, 'ab', 'cd', 'ef'] Here is my json encoded object: [2, 4, 5, "ab", "cd", "ef"] Here is JSON back to a data structure: [2, 4, 5, 'ab', 'cd', 'ef']
In the above code, we first take the object (my list) and use the “dumps” method to returns a string and then to load JSON back to a data structure we use the “loads” method that turns a string and turns it into the JSON object data structure.
- Related Articles
- Python object serialization (Pickle)
- Internal Python object serialization (marshal)
- C# object serialization
- PHP Object Serialization
- Object Graph in Java Serialization
- Object Serialization with inheritance in Java
- Object Serialization with Inheritance in Java Programming
- How to include Text object's default values in its serialization using FabricJS?
- FabricJS – How to include a Line object's default values in its serialization?
- What is Serialization in Java?
- Serialization and Deserialization in C#
- What is Binary Serialization and Deserialization in C# and how to achieve Binary Serialization in C#?
- What is serialization in C#.NET?
- Difference between Serialization and Externalization in Java
- How transient works with final in Java serialization?
