Pickle vs XML for file I/O

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Aug 1 02:13:12 EDT 2006


In <op.tdk6cd0pqaun6z at emac.local>, crystalattice wrote:

> On Mon, 31 Jul 2006 14:35:39 -1000, Simon Forman <rogue_pedro at yahoo.com>  
> wrote:
> 
>> What kind of trouble were you having with pickle?
>
> It's mostly a combination of things (I hope you can follow my logic).   
> First, to use "good programming practice", I want to implement a  
> try/except block for opening the pickle file.  But I can't figure out if  
> this block should be included outside the classes, included in just the  
> base class, or if the base and subclasses need it; I'm leaning to putting  
> outside the classes as a global method.

That's not a problem with pickle but where you think a potential IO error
is handled best.  Ask yourself if you can do something sensible at the
point where you catch an exception.  If you don't know what to do with it,
don't catch it and just let it propagate.

> When pickling a class instance, is the pickle statement placed within the  
> class (say, at the end of the class) or is it where the instance is  
> created, such as a test() method?

`pickle.dump()` and `pickle.load()` are not statements but just functions.
And you should place a call where you actually want to save or load
instances in your program flow.

> Plus, to modify data in a class, do I have to unpickle the whole thing  
> first or is there a way to modify the data while it's pickled?  Actually,  
> I think I can answer that last question:  a character instance, having  
> been created, will stay resident in memory until the player quits,  
> character dies, or otherwise is no longer needed.  At that point the  
> character instance should be pickled (if necessary) to disk; pickle  
> shouldn't be used while data modification is required and the class  
> instance is "active", correct?

Yes that's correct.  It wouldn't be different with XML.

> I also remember reading on a thread here that pickle sometimes has issues  
> when used with classes, which makes me hesitant to pickle an entire class  
> instance.  That's why I thought XML may be safer/better.

You can't pickle everything.  Files for example are unpickleable and you
can't pickle code so pickling classes is not that useful.

An advantage of `pickle` is that it's in the standard library and ready to
use for serialization.  To get an XML solution you must either write your
own serialization code or use a 3rd party library.

What are the problems you fear when using `shelve` by the way?

Ciao,
	Marc 'BlackJack' Rintsch




More information about the Python-list mailing list