467,075 Members | 1,134 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Triple quoted repr

On 2003-09-04, Rasmus Fogh said:
I need a way of writing strings or arbitrary Python code that will

a) allow the strings to be read again unchanged (like repr)
b) write multiline strings as multiline strings instead of escaping
the \n's.

A repr function that output triple-quoted strings with explicit
(non-escaped) linebreaks would be perfect.

Failing that, what is the simplest way to get close to the goal?


There were no responses. Anyone have an answer?
Jul 18 '05 #1
  • viewed: 2063
Share:
1 Reply

"Edward C. Jones" <ed******@erols.com> wrote in message
news:40**********************@news.rcn.com...
On 2003-09-04, Rasmus Fogh said:
> I need a way of writing strings or arbitrary Python code that will
>
> a) allow the strings to be read again unchanged (like repr)
> b) write multiline strings as multiline strings instead of escaping
> the \n's.
>
> A repr function that output triple-quoted strings with explicit
> (non-escaped) linebreaks would be perfect.


Other have given you possible solutions. Some comments:

While there are triple-quoted and possibly multiline string *literals*,
there are no triple-quoted or multiline string *objects* any more than
there are raw-string objects. All 'types' of (byte) string literals are
converted to the one and same (byte) string object type. Once a string
object is initialized, the history of how it came to be (which also could
be as an expression instead of literal) is lost.

In particular, while chr(10) most usually arises from a literal linebreak
and is intended to be interpreted as such, this is not always so. For
instance, in a string of bytes, each of which represents an integer in
range(256), chr(10) represents 10, not 'newline'.

Repr does something simple that meets the criterion (a) of evaluability.
In doing so, it avoids problems with the fact that 'explicit (non-escaped)
linebreaks' are (most unfortunately) system dependent. As it is now, you
can, I believe, transmit repr(s) on machine x to machine y, eval it on b,
and dependibly get back s. At least before the recent addition of the
universal newline facility, I believe that doing b) would have made this
undependable (ie, broken a)).

Terry J. Reedy


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anthony Roberts | last post: by
8 posts views Thread by Christoph Zwerschke | last post: by
11 posts views Thread by bearophile | last post: by
3 posts views Thread by Michele Simionato | last post: by
14 posts views Thread by dmh2000 | last post: by
7 posts views Thread by p.lavarre@ieee.org | last post: by
2 posts views Thread by Mark Tolonen | last post: by
8 posts views Thread by Lawrence D'Oliveiro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.