Object Persistence and Pickle&zlib

Greg Ewing greg.ewing at compaq.com
Tue Nov 16 15:44:18 CET 1999


thomas at bibsyst.no wrote:
> 
> class some_object:
>     title = ''
>     id = 0
>     items1 = {}
>     items2 = {}
>     items3 = {}
>     items4 = {}

That's going to give you four dictionaries stored as class attributes,
which are shared between all instances -- probably not what you want.
To give each instance its own set of dictionaries you'll have to
create them in the initialisation method:

   class some_object:
      title = ''
      id = 0

      def __init__(self):
         self.items1 = {}
         self.items2 = {}
         self.items3 = {}
         self.items4 = {}

That's also the reason your dictionaries weren't getting pickled.
Pickling an instance only saves instance attributes, not class
attributes.

> Some of the methods
> are available, but some are missing.

If you're reading the pickled data using a different program from
the one you wrote it with, check that you've defined all the
methods in the second program. Methods, being class attributes,
don't get pickled either -- it's up to the unpickling program
to supply them.

Hope that helps,
Greg




More information about the Python-list mailing list