Python serialization - Why pickle?

kiriloff picture kiriloff · Jan 23, 2012 · Viewed 49.7k times · Source

I understood that Python pickling is a way to 'store' a Python Object in a way that does respect Object programming - different from an output written in txt file or DB.

Do you have more details or references on the following points:

  • where are pickled objects 'stored'?
  • why is pickling preserving object representation more than, say, storing in DB?
  • can I retrieve pickled objects from one Python shell session to another?
  • do you have significant examples when serialization is useful?
  • does serialization with pickle imply data 'compression'?

In other words, I am looking for a doc on pickling - Python.doc explains how to implement pickle but seems not dive into details about use and necessity of serialization.

Answer

austin1howard picture austin1howard · Jan 23, 2012

Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.

As for where the pickled information is stored, usually one would do:

with open('filename', 'wb') as f:
    var = {1 : 'a' , 2 : 'b'}
    pickle.dump(var, f)

That would store the pickled version of our var dict in the 'filename' file. Then, in another script, you could load from this file into a variable and the dictionary would be recreated:

with open('filename','rb') as f:
    var = pickle.load(f)

Another use for pickling is if you need to transmit this dictionary over a network (perhaps with sockets or something.) You first need to convert it into a character stream, then you can send it over a socket connection.

Also, there is no "compression" to speak of here...it's just a way to convert from one representation (in RAM) to another (in "text").

About.com has a nice introduction of pickling here.