[Tutor] Unpickling a Pickled Object

Jethro Cramp jsc@rock-tnsc.com
Sat, 5 May 2001 12:21:10 -0800


On Friday 04 May 2001 12:19 am, Learn Python wrote:
> hi Jethro,
>
> c'd you please tell why you think the __init__ s'd'nt be called?
> If i'm doing some initialisation in __init__
> whch in turn will be used by other methods then the unpickling w'd'nt
> be of much use right?
> my code might fail bcos certain things did'nt get initialized.
> Do we have a
> private void readObject() java equivalent in python? which w'd
> automatically get called when we "deserialize" the object / in this case
> "unpickle". Then we can probably initialize some stuff there?
>
> karthik
>
Dear Karthik,

As I understand it when you pickle an object you are infact saving the 
object's state. The reason I asked (and hoped that _init_ isn't called when I 
unpickle an object) is that in the class I was designing I want to set an 
IsValid property to false (because when the class is instantiated it will be 
in an invalid state) and a list of properties that are an invalid state. When 
the object gets serialized it can either be in a valid or invalid state, and 
a list of the properties that are still invalid are serialized with it.  When 
the object is unpickled (in my mind) it should be in the same state as when 
it was pickled. Running __init__ when it is unserialised might change that. 
I didn't want to write code in __init__ to check for validity. Not a big deal 
to implement, but I am LAZY, after all ;). 

Sorry I don't know anything about JAVA and I'm not much of a programmer so I 
can't answer your question about readObject().  Maybe someone wiser on this 
list can give you an answer

Jethro