[Tutor] (no subject)

fleet@teachout.org fleet@teachout.org
Mon, 3 Dec 2001 15:51:31 -0500 (EST)


Your example below works; but when I try something like:

john = class('john doe','123-456-6789')
jane = class('jane doe','123-456-9876')

I can pickle john, or I can pickle jane; but I don't seem to be able to
pickle both.  I'm assuming to do this I need to store the data in a
dictionary (or something) and pickle the dictionary (or something).

Actually, having pondered this through three screensaver activations (or
so) it appears I would have to store the *instance* in a dictionary - not
the name or phone number; but just "john."

>>> john
<addrbook.AddrBookEntry instance at 80e62b0>

But would this be valid for different sessions of Python?

				- fleet -

dman <dsh8290@rit.edu said:

>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