pickle.load() on an dictionary of dictionaries doesn't load full data structure on first call

James Pearson xiong.chiamiov at gmail.com
Sun Feb 22 22:58:27 EST 2009

Ah, thank you, you explained that quite well and opened my eyes to some
things I very much need to improve in my code.  I'll keep those
list-etiquette things in mind next time.

On Sun, Feb 22, 2009 at 5:10 PM, Albert Hopkins <marduk at letterboxes.org>wrote:

> On Sun, 2009-02-22 at 16:15 -0800, James Pearson wrote:
> > I've been using irclib to write a simple irc bot, and I was running
> > into some difficulties with pickle.  Upon some experimentation with
> > pdb, I found that pickle.load() doesn't load *all* of the data the
> > _first_ time it's called.
> >
> > For instance, I have this dictionary pickled:
> > {'xiong_chiamiov': {'name': 'James Pearson', 'distro': 'Arch'}}
> > The first time I pickle.load() it, I get this:
> > {'xiong_chiamiov': {'name': 'James Pearson'}}
> > The 2nd time, I get the full thing.
> >
> > Is this a bug in pickle, something wrong with the way I'm approaching
> > this, or what?  Full code is available at:
> > http://github.com/xiongchiamiov/mpu/
> >
> In general it's a bad idea to point the list to a full.  It's usually
> wiser to deduce the issue to a relatively small, easily runnable,
> snippet of code and then paste that snippet in your post.  Readers
> usually don't want to have to read through your entire program to
> understand what you are asking them.
> Having said that, pickle is for serializing data.  You shouldn't be
> opening a file in read/write/append mode and dumping and loading to the
> same file object.  It's not a multi-access database like a DBMS is.  The
> basic idea is:
>     1. open (empty) file write-only.
>     2. dump object(s) to file.
>     3. close file
> or
>     1. open file read-only
>     2. load object(s) from file
>     3. close file
> Anything other than the two may lead to undesired/undefined behavior.
> But the likely answer to your question is that your pickle file has (at
> least) 2 objects stored in it.  And so on each load you get subsequent
> objects.  That's probably the "undesired" affect of opening the file in
> append mode.
> --
> http://mail.python.org/mailman/listinfo/python-list

James Pearson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090222/a05c89d9/attachment.html>

More information about the Python-list mailing list