DEV Community

Cover image for Object Serialization and Deserialzation in Python
Adarsh Rawat
Adarsh Rawat

Posted on

Object Serialization and Deserialzation in Python

In this Article we're going to see :-

serializing object *(a process called serialization)
de-serializing object *(a process called de-serialization)

let's start with the definations :-

Serialization :-

Serialization is the process of converting data structures or object state into a format that can be stored or saved in memory for latter use, that format in which the object state saved will follow some rules which will help in de-serializing the object later.

De-serialization :-

De-serialization is just opposite of serialization in De-serialization ,we get the object state back to recreate the original object from the serialized format.

Now that we have understand defination of serialization and de-serialization.

let's see some place where they are very helpful :-

  • While sending data over the internet the data is transfered mostly in Json form (JSON is a format that encodes objects in a string and deserialization it (convert string -> object))

  • Hugh Machine Learning Model that are trained on hugh amount of data ,need to stored in some form for later use ,we cannot re-train them again and again , that is where serialization help
    to store ML model state.

  • Big Data system uses serialization and deseralization to store large amount of data.

we have seen only few applications, serialization and deserialization are pretty much applied whererever there is need to store data in some form to use it for later use.

Now we going to look how to do Serialzation and Deserializatiom in Python

Pickle :-

Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk.
Pickling is a way to convert a python object (list, dict, etc.) into a character stream
It saves the find the .pkl format

methods available :-

  1. dump() − The dump() method serializes to an open file (file-like object).

  2. dumps() − Serializes to a string

  3. load() − Deserializes from an open-like object.

  4. loads() − Deserializes from a string.

Serializing the object

import pickle
class Student:
  def __init__(self,firstname,lastname,age,standard):
    self.firstname = firstname
    self.lastname = lastname
    self.age = age
    self.standard = standard

  def showinfo(self):
    print(f"Firstname :- {self.firstname}")
    print(f"Lastname :- {self.lastname}")
    print(f"Age :- {self.age}")
    print(f"Standarad :- {self.standard}")


student1 = Student("Adarsh","Raven",21,"12th")
student2 = Student("Ankit",'Raven',24,'11th')


# Student Info
print("Student1 :- ")
student1.showinfo()


print("\nStudent2 :- ")
student2.showinfo()

# Serializing object
picked_student1 = pickle.dumps(student1)
picked_student2 = pickle.dumps(student2)

# Object stored in Byte steam
print("serialized student",picked_student1)


Output :-

Student1 :- 
Firstname :- Adarsh
Lastname :- Raven
Age :- 21
Standarad :- 12th

Student2 :- 
Firstname :- Ankit
Lastname :- Raven
Age :- 24
Standarad :- 11th
serialized student b'\x80\x03c__main__\nStudent\nq\x00)\x81q\x01}q\x02(X\t\x00\x00\x00firstnameq\x03X\x06\x00\x00\x00Adarshq\x04X\x08\x00\x00\x00lastnameq\x05X\x05\x00\x00\x00Ravenq\x06X\x03\x00\x00\x00ageq\x07K\x15X\x08\x00\x00\x00standardq\x08X\x04\x00\x00\x0012thq\tub.'

Enter fullscreen mode Exit fullscreen mode

Deserializing the same object

orignal_student1 = pickle.loads(picked_student1)
orignal_student2 = pickle.loads(picked_student2)

print("\n\nAfter Getting object back from the saved state :- \n")
print("Student1 :- ")
orignal_student1.showinfo()

print("\nStudent2 :- ")
orignal_student2.showinfo()

Output :-

After Getting object back from the saved state :- 

Student1 :- 
Firstname :- Adarsh
Lastname :- Raven
Age :- 21
Standarad :- 12th

Student2 :- 
Firstname :- Ankit
Lastname :- Raven
Age :- 24
Standarad :- 11th
Enter fullscreen mode Exit fullscreen mode

there are also other ways available to do the same task
we have covered in the above example, it will mention resources to those if you're intrested :-

:-)

Top comments (0)