Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 inde-serializing the object later.

De-serialization :-

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

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

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

  • While sending data over theinternet the data is transfered mostly inJson 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 storeML model state.

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

we have seen only few applications,serialization anddeserialization 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 doSerialzation 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 pickleclass 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 Infoprint("Student1 :- ")student1.showinfo()print("\nStudent2 :- ")student2.showinfo()# Serializing objectpicked_student1 = pickle.dumps(student1)picked_student2 = pickle.dumps(student2)# Object stored in Byte steamprint("serialized student",picked_student1)Output :-Student1 :- Firstname :- AdarshLastname :- RavenAge :- 21Standarad :- 12thStudent2 :- Firstname :- AnkitLastname :- RavenAge :- 24Standarad :- 11thserialized 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 modeExit 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 :- AdarshLastname :- RavenAge :- 21Standarad :- 12thStudent2 :- Firstname :- AnkitLastname :- RavenAge :- 24Standarad :- 11th
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

An aspiring software developer
  • Joined

More fromAdarsh Rawat

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp