[Python-3000] Removing repr

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Apr 7 02:23:42 CEST 2006


Michael P. Soulier wrote:

> I thought that the distinction was as follows. 
> 
> str() should return a human-readable representation of something. 
> 
> repr() should return a machine-readable representation of something, suitable
> for an exec command. 

As Michael pointed out, this is totally impractical
in the majority of cases.

The guideline I use is quite different: str() is for
normal program output, repr() is for debugging
output.

To expand a bit more: repr() should return something
that unambiguously indicates the *type* of the
object. If something about the value can be indicated
as well without being too verbose, that's a bonus,
but it's not the most important thing.

I suggest that the passage in the docs about
repr() output being eval-able be deleted, since
it misleads more than it helps, and replaced with
something to the above effect.

BTW, these days I normally don't bother giving my
classes a __repr__ at all, and just make do with
the default <someclass instance at 0xXXXXX>. If I
want to know anything more, I insert prints at the
appropriate place to probe the object further.

Anyway, -1 on combining them. In my view they
*do* serve two very clearly distinct purposes --
just not the ones the docs say!

--
Greg


More information about the Python-3000 mailing list