Basic Serialization - a design decision question

Robert Kern robert.kern at gmail.com
Fri Apr 6 15:23:54 EDT 2007


Gizmo wrote:
> Hello
> I am a relative newcomer to Python, and I am studying it to understand
> its design. It intrigues me.
> I recently studied Serialization of classes via the pickle/cPickle
> library, and I have a question.
> 
> Why is Serialization handled by a separate library (ie, pickle). Is it
> possible, by design, to have serialization "internally" implemented via
> an implicit ___serialize___ method? Ofcourse, you have to make this
> method not overrideable (sp?). For example, the __repr__ method gives us
> the string representation of a class... similarly, the __serialize__
> method would give us the "serial norm" representation of the class.
> 
> This would allow me to do something like this,
> conn.send(serial(myClass));  // or something like that?

Serialization is a complicated task. It needs at least a full module devoted to
it just in terms of being able to implement it. Trying to stuff all of it into a
builtin is asking for trouble. Of course, you can always do this:

  from cPickle import dumps
  ...
  conn.send(dumps(myClass))

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list