Triple quoted repr
tjreedy at udel.edu
Thu Jun 3 20:44:17 CEST 2004
"Edward C. Jones" <edcjones at erols.com> wrote in message
news:40be9310$0$2979$61fed72c at 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
More information about the Python-list