473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 ) it looks like
everything has worked well, but in the saved file 'test.pickle' only the
list 'build_steps' is saved - the dictionary 'configuration' is missing.
There is wether an error-message nor an exception.

When I try to save only the dictionary, there is no problem at all - the
dict is saved to the file.

I also tried the 3 different protocols (0, 1, 2), but none of them worked
for me.

I hope somebody knows what to do ;)

Thanks for reading

Marco
Oct 12 '06 #1
6 1230

Marco Lierfeld wrote:
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 ) it looks like
everything has worked well, but in the saved file 'test.pickle' only the
list 'build_steps' is saved - the dictionary 'configuration' is missing.
There is wether an error-message nor an exception.

When I try to save only the dictionary, there is no problem at all - the
dict is saved to the file.

I also tried the 3 different protocols (0, 1, 2), but none of them worked
for me.
At a wild guess. Since pickle descends the objects hierarchy, and since
configuration and build_steps aren't local to an instance of a class,
it stores only a reference to them (so you won't see values). However,
if you change the above to:

class subproject:
def __init__(self):
configuration = { }
build_steps = [ ]

That'll probably be what you expect...

Jon.

Oct 12 '06 #2

Jon Clements wrote:
if you change the above to:

class subproject:
def __init__(self):
configuration = { }
build_steps = [ ]
Of course, I actually meant to write self.configuration and
self.build_steps; d0h!

Oct 12 '06 #3
"Marco Lierfeld" <ma************@rwth-aachen.dewrote in message
news:4p************@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

Oct 12 '06 #4
Jon Clements wrote:
>if you change the above to:

class subproject:
def __init__(self):
configuration = { }
build_steps = [ ]

Of course, I actually meant to write self.configuration and
self.build_steps; d0h!
Thank you Jon and Paul, you both were 100% right :)

But I still don't understand, why the list was saved and the dict was not...
confusing ;)

Bye,
Marco
Oct 12 '06 #5
Marco Lierfeld wrote:
Jon Clements wrote:
>>if you change the above to:

class subproject:
def __init__(self):
configuration = { }
build_steps = [ ]

Of course, I actually meant to write self.configuration and
self.build_steps; d0h!

Thank you Jon and Paul, you both were 100% right :)

But I still don't understand, why the list was saved and the dict was
not... confusing ;)
Chances are you have inadvertently created an /instance/ attribute
build_steps which was then saved:

s = subproject()
# ...
s.configuration["name"] = "my dinner" # modifies the class attribute
s.build_steps = ["hunt", "kill", "cook"] # creates an instance attribute

Peter
Oct 12 '06 #6
Peter Otten wrote:
Chances are you have inadvertently created an /instance/ attribute
build_steps which was then saved:

s = subproject()
# ...
s.configuration["name"] = "my dinner" # modifies the class attribute
s.build_steps = ["hunt", "kill", "cook"] # creates an instance attribute
Yes, now I see. That's the way I filled the dict and the list.

Thank you for the explanation :)

Marco
Oct 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Guettler | last post by:
Hi! If I pickle a class which inherits from dict, I don't get the same data again, if I unpickle it. def test_pickle(): home="..." server=WorkflowServer(home) server.save()
1
by: Thomas Guettler | last post by:
Hi! After unpickling the objects are not the same any more. Is this a bug or feature? import pickle class MyDictContainer(dict): def __init__(self): dict.__init__(self)
1
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}}...
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
1
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
3
by: wolfgang.lipp | last post by:
some time after posting my `Linkdict recipe`__ to aspn__ -- basically, a dictionary with run-time delegational lookup, but this is not important here -- i thought gee that would be fun to make...
2
by: Peter Bengtsson | last post by:
Hi, I'm trying to pickle an object instance of a class that is like a dict but with a __getattr__ and I'm getting pickling errors. This works but is not good enough. $ python2.4 .... pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.