Explain pickling and unpickling in python.

Pickling and Unpickling:
  • Pickle is a standard module which serializes and deserializes a python object structure.
  • Pickle module accepts any python object, converts it into a string representation and dumps into a file(by using dump() function ) which can be used later this process is called pickling.
  • Unpickling is a process of retrieving original python object from the stored string representation for use.
  • Pickling is a process whereby a python object hierarchy is converted into a byte stream and unpickling is the inverse operation, whereby a byte stream is converted back into an object hierarchy.
  • Ex:
import pickle
a = [ "apple" , "banana" ]
file_Name = "abc"

# open the file for writing
fileObj = open( file_Name , "wb" )

# This writes the object a to the file
pickle.dump( a , fileObj)
fileObj.close()
fileObj = open( file_Name , "r")

# load the object from the file into variable
b = pickle.load( fileObj )

print( a == b )  # True

Comments

Popular posts from this blog

What is composite key?

What are the different data types in Python?

What is __repr__ function?