[Python-3000] PEP 3138- String representation in Python 3000

Jim Jewett jimjjewett at gmail.com
Tue May 27 20:53:25 CEST 2008


On 5/26/08, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Jim Jewett wrote:

> > The problem is classes where str(x) != repr(x), and how they get
> > messed up when a container holding (one of their) instances is
> > printed.

> This is NOT a bug, since str([1, 2, 3]) and str(list("123")) SHOULD produce
> results that look different.

Ideally, but it isn't that important.  repr(1) and repr("1") need to
be different, but if str(1) and str("1") look alike, that is
acceptable.  It already happens with

     >>> "%s %s " % (1, "1")

> Calling str() internally to display the
> contents of containers is a broken idea.

If you are using it for precise debugging, you should use repr -- and
repr should be used all the way down.  If you're using str because you
want (fairly) readable output, then str should be used all the way
down.

> The ambiguity that recursive calls
> to str() would introduce would make any concerns about potential confusion
> between different Unicode glyphs seem utterly inconsequential.

So don't do it on repr -- do it only on str -- but always do it on
str, even when str falls back to repr for a particular level of
recursion.

-jJ


More information about the Python-3000 mailing list