[Tutor] class data

dman dsh8290@rit.edu
Mon, 3 Dec 2001 13:13:26 -0500


On Mon, Dec 03, 2001 at 12:52:15PM -0500, fleet@teachout.org wrote:
| This was my understanding of it also; but classes apparently contain their
| own __dict__.  

Classes have a dictionary that only contains the methods and class
members.  Class _instances_ have a __dict__ that contains the data
stored by the instance.

| I was kind of hoping it would get pickled along with the rest. 

It does -- it is part of the data.

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

The same way you always do.  Here's an example (that skips the file
read/write step) :


import pickle

class C :
    pass

o = C()
o.foo = "bar"
o.spam = "eggs"

pickled_string = pickle.dumps( o )

# normally you would write this string to a file, or a socket, or something

del o # just to make sure the old one is gone and I can't make a typo

# normally you would read the string from a file, or a socket, or something
obj = pickle.loads( pickled_string )

print obj.foo , obj.spam


-D

-- 

(E)very (M)inor (A)ttention (C)osts (S)anity