Is there any way to make repr(aString) use double quotes?

François Pinard pinard at iro.umontreal.ca
Sat Apr 12 14:51:52 CEST 2003


[Jeremy Fincher]

> Francois Pinard <pinard at iro.umontreal.ca> wrote in message news:<mailman.1049836294.24621.python-list at python.org>...
> > # Python delimits a string it by single quotes preferably, unless
> > # single quotes appear within the string while double quotes do
> > # not, in which case it uses double quotes for string delimiters.
> > # Checking the string contents, the C code stops at the first NUL.
> > # We prefix the string with a single quote and a NUL, this forces
> > # double quotes as delimiters for the whole prefixed string.  Then,
> > # we get rid of the representation of the single quote and the NUL.
> > 
> > write('"' + repr("'\0" + value)[6:])

> Ah, perfect!  Thanks :)

You are welcome, of course.  But this may not be perfect! :-) We are relying
here on an undocumented behaviour of Python, and there is no reasonable[1]
guarantee that some later version of Python will not break the above code.
So in practice, it is only usable as a short or medium term kludge, that you
should be prepared to _happily_ revise if the need ever arises.

--------------------
[1] What means "reasonable"?  It is a difficult concept.  To illustrate it a
tiny bit, consider the timely finalisation of Python objects.  This is a
somewhat undocumented behaviour for which we have reasonable guarantee that
it will never change (in the official, standard Python).

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list