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

Jim Jewett jimjjewett at gmail.com
Tue May 27 21:08:18 CEST 2008


On 5/26/08, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Jim Jewett writes:

>   > The only reason for this change is that __repr__ gets used when
>   > __str__ *should* be used instead.

> That's not what the advocates say.

I still haven't seen a use case where it *should* be using repr *and*
needs to print outside of ASCII.

There are plenty of cases where it *is* using repr because
str(container) fell back to repr, and then the contained strings stay
in repr instead of shifting back to str.  I just haven't seen any
where repr is the *right* function, as opposed to what they're stuck
with because a container doesn't implement a separate __str__.  [The
file exceptions *may* be a separate case, because of tracebacks using
repr to print, but I'm not sure even there.]

> Well, in `String Conversions', the manual says """In particular,
>  converting a string adds quotes around it and converts
>  "funny" characters to escape sequences that are safe to print."""

>  Now, I agree with you about what's "safe".  However, in a text-
>  processing application in a Japanese environment, that's hardly
>  useful, and our Japanese programmer can argue that in his environment,
>  printing all of Unicode *is* safe.

I think he or she will still be wrong, because of confusables -- it is
just that "unsafe" characters are far more rare (since byte value
alone isn't a problem) and the cost of not printing non-ASCII
characters is higher.

So I suggest that he or she use str, rather than repr -- and that we
fix containers to make this possible.

>   > Again -- *why* is repr used instead of str?

> I don't use it myself other than as a way of diagnosing bugs in
>  programs I write or maintain; in personal practice, I'm in your camp.
>  But my understanding is that there is often an intermediate level,
>  such as a website admin, who needs *some* of the precision of repr()
>  such as escaped representation of whitespace, but also needs to be
>  able read most of the output.

Could someone who does need this explain more?

I understand wanting the two side-by-side.  I sometimes want that with hex.

I understand wanting a container's contents to be readable.  I realize
you can't easily get that today, and consider that a bug.  (Nick's
disagreement noted.)

I don't understand needing *exactly* whitespace escaped, but not, say,
stray characters from scripts you've never used, even though the rest
of the page *is* in an expected script.

-jJ


More information about the Python-3000 mailing list