[Tutor] class data

alan.gauld@bt.com alan.gauld@bt.com
Tue, 4 Dec 2001 10:47:36 -0000


> This was my understanding of it also; but classes apparently 
> contain their own __dict__.  

Yes but mostly you don't need to know that :-)

You create instances of your classes ad thise instances 
get a copy of the dictionary internally. They then populate 
their copy with data and when you pickle the object(instance) 
it saves the internal dictionary.

When you restore from a pickle the new object(instance) 
knows what the dictionary should look like because it 
is defined in the class(template).

> If it did, I have no clue how to access the data.

You don't pickle classes you pickle instances.
You restore those instances and access the internal data
as if you had never pickled it!

foo = MyClass()
foo.bar = 42

#  pickle foo here

foo = 7    # temporarily make foo something else if you like...

# now restore foo from the pickle

print foo.bar   # viola! foo has its object properties restored 

Its as if foo had never been away. You just access the fields
as you would have done before pickling.

Alan G.