eval(repr(x)) == x

Oren Tirosh oren-py-l at hishome.net
Sun Jan 27 10:27:15 EST 2002


On Sun, Jan 27, 2002 at 08:33:28AM -0500, François Pinard wrote:
> I wonder if `repr' could not be made to produce a string which includes
> a non-binary pickle of a complex object, combined with a modification to
> the standard Python reading device understanding the convention, and able
> to recreate the original from the pickle data in the string.

No need to modify Python for this.  It all depends on you and how you write
your object. Just make sure the entire object state can be reconstructed 
using the arguments to its constructor and write a __repr__ method that 
prints out this constructor call.  As long as your objects are purely
hierarchical it can even work recursively.

> Yes, it traversed my mind that `repr' of a complex object, containing
> a representation of it, could use a lot of characters, maybe enough to
> kill legibility.  I was probably hoping a miracle :-).  Some happen!

C'est un miracle! The pprint module!

Even without such miracles, repr output is far more legible than pickle.  
I don't think repr can be or should be a general purpose replacement for 
pickling, but there are some places where it can serve a similar function.

	Oren





More information about the Python-list mailing list