Pickling in Python

Pickling is used for the serialization and deserialization of the Python objects.

In layman terms, the process of converting the python object to stream of bytes is called pickling and the process of converting the stream of bytes to python object is called unpickling.  

Python pickles to object in format that is Python-specific and other non-python programs cannot  reconstruct pickled Python objects.

Python provides a module pickle for serialization and deserialization of the objects.

Pickle provides mainly two protocols to choose for serialization.

    pickle.HIGHEST_PROTOCOL

  •            New style class are handled more efficiently.

    pickle.DEFAULT_PROTOCOL

  •            Old binary format serialization which is also compatible with earlier versions of Python.

pickle provides a dump function to serialize the objects and a load function to de-serialize.

pickle.dump(object, file_obj, protocol)

This function takes three arguments

  1. Python object to serialize.
  2. File object  in which the serialized python object must be stored.
  3. Protocol (If a protocol is not specified, protocol 0 is used)

Here is a quick example for python Pickle

import pickle

class DemoClass(object):

      def __init__(self, name, age, numbers=[]):
           self.name = name
           self.age = age
           self.contact_numbers = numbers

       def display_name(self):
           print(" Name %d" % self.name)

       def display_age(self):
           print("Age %d" % self.age)


if __name__ == "__main__":

    # create instance of the above declared class
    obj = DemoClass('Example', '22', ['1234567890', '0987654321'])

    # File object in which serialized object must be stored.
    file = open('dump_file', 'wb')

    # dump the above created instance into the file
    pickle.dump(obj, file, pickle.HIGHEST_PROTOCOL)

    # close the file
    file.close()

pickle.load(file_obj)

Function load is used to de-serialize the Python object. It expects one argument, file_object in which object was serialized in write mode.

Below is a quick example to un-pickle the python object with reference to the above code.

# opening the file in which object was dumped.
file = open(dump_file, 'rb')

# Get the un-pickled instance
data = pickle.load(file)

# close the file
file.close()

print('printing the un-pickled instance:')
print(" Name %s" % data.name)
print("Age %d" % data.age)
print("Number {}".format(",".join(data.contact_numbers)))

That’s it. Thanks for reading!

About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at bootstrap@causecode.com
Have you subscribed to our blogs and newsletter? If not, what are you waiting for?  Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives