Data::Dumper for Python

Dave Benjamin ramen at lackingtalent.com
Thu Oct 28 16:08:04 EDT 2004


In article <pan.2004.10.28.21.36.02.724653 at jerf.org>, Jeremy Bowers wrote:
> On Thu, 28 Oct 2004 18:06:12 +0000, Dave Benjamin wrote:
> 
>> Take a look at the "pprint" module. Also, it's worth noting that the Python
>> interpreter prints representations of data structures all the time, no
>> library required:
> 
> However, Python tries ***much*** less hard to make those representations
> actually evaluate back to equivalent objects. Based on my experiences but
> with no particular knowledge of the history of the two languages in this
> regard, this is because Python makes many manipulations easy that are hard
> to borderline impossible in Perl, and it is much harder to create such
> representations in general.
> 
> As a result, repr of any but the most base classes is often used more
> for "debugging style" info, and str for a simple identification. It is not
> safe in general to eval(repr(obj)) and expect anything but a Syntax Error.
> 
> Python shuffles that task off to the Pickle module, which is what you'd
> want to look up in the docs. That splits human representation off from
> computer-reproducable representation, and I believe overall this is
> superior; the two are not the same. In addition, we then get the Pickle
> protocol extensions which are frequently quite handy, especially when
> dealing with weakrefs.

Very good points. My experience with Data::Dumper in Perl has only been with
debugging/pretty-printing; I've never used it as a serialization technique.
If you are the author of all of the classes in your data representation, you
could (in theory) design it such that eval(repr(obj)) always evaluates to
obj, but Pickle is more likely what you want anyway.

-- 
 .:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
        "talking about music is like dancing about architecture."



More information about the Python-list mailing list