Pickling an instance of a class containing a dict doesn't work

Paul McGuire ptmcg at austin.rr._bogus_.com
Thu Oct 12 10:15:48 EDT 2006


"Marco Lierfeld" <marco.lierfeld at rwth-aachen.de> wrote in message 
news:4p6vk6Fh2jevU1 at news.dfncis.de...
> Hello there,
>
> I want to save an instance of a class containing a dictionary with the
> pickle-module.
>
> The class looks like this:
>        class subproject:
>                configuration   = {}
>                build_steps     = []
>                # some functions
>                # ...
>
> Now I create an instance of this class, e.g.
>        test = subproject()
> and try to save it with pickle.dump(test, file('test.pickle','wb')) or 
> with
> pickle.Pickler(file('test.pickle','wb')).save(test)

I'm guessing that configuration and build_steps are supposed to be instance 
variables, not class-level variables.  It would be interesting to see what 
your __init__ method looks like.  I'm guessing you assign something the 
self.build_steps in __init__, but self.configuration is omitted.

Try moving these initializers into the __init__ method, as:
        class subproject:
                def __init__(self):
                        self.configuration   = {}
                        self.build_steps     = []

and see if pickle starts behaving better.

-- Paul






More information about the Python-list mailing list