eval(repr(x)) == x

Alex Martelli aleax at aleax.it
Sun Jan 27 10:25:05 EST 2002


On Sunday 27 January 2002 16:07, Oren Tirosh wrote:
> On Sun, Jan 27, 2002 at 11:14:47AM +0000, Alex Martelli wrote:
> > Oren Tirosh wrote:
> > > For many python builtins eval(repr(x)) == x.  I find this property
> > > very useful for debugging and writing tests.  In some cases I write
> > > my own classes so their repr is an expression that recreates their
> > > state.
> >
> > It's sort of nice when it works, but I don't quite see how this
> > translates into "very useful for debugging and writing tests". 
> > Examples please?
>
> A unit test often consists of a sequence of method calls and their
> expected results.  Writing these expected results is a pain in the a**. A
> relatively easy way to generate the expected result is to construct it in
> an interactive session, print it and then select and paste to the test
> code. But this depends on eval(repr(x))==x.

I still can't see it.  I generally use doctest for unit tests, and it does 
lend itself to working that way (just cutting and pasting an interactive
session) without dependency on eval and repr being inverse.  The
*only* p.i.t.a with doctest is that with every Python release the exact
wording of some exception or other changes, so that all tests that
involve that exception "break" until they've been 'fixed' to reflect the
wording in the new version (and then you can't run them under the
previous version any more) -- basically it's very impractical for test
suites intended to survive from one Python version to the next, or
work across two Python versions.  But that wouldn't be fixed by any
change to repr...


Alex




More information about the Python-list mailing list