Object "dumping"
Andrew Bennetts
andrew-pythonlist at puzzling.org
Sun Feb 2 22:54:13 EST 2003
On Mon, Feb 03, 2003 at 11:47:38AM +1000, Derek Thomson wrote:
> Hi all,
>
> I'm debugging a particularly nasty problem in a moderately complex bit
> of Python, and I really need something that I can use to just print out
> the state of an object.
>
> I also do a lot of Perl, and that's available in Perl with the
> Data::Dumper module.
>
> I know Python has pprint, but that stops when it encounters an object
> ie. it only dumps sequences and hashes, which is less than what I want.
The problem is harder than it seems... simply recursively printing the
objects and their contents won't work for cyclic objects, so for many data
structures, you *need* something that can cope with this, like pickle.
So, if you simply want to dump the objects to a file, and read them back in,
and don't care about human-readability, use pickle.
If you want to read it too, I'd recommend twisted.spread.jelly, from
Twisted. It's an s-expression based serialisation format that essentially
does the same job as pickle, but is human readable -- particularly if you
run it through pprint <wink>. I've used it a couple of times for debugging.
There are other pickle alternatives too, like an XMLPickle, iirc.
-Andrew.
More information about the Python-list
mailing list