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

Francois Pinard pinard at iro.umontreal.ca
Tue Apr 8 17:10:09 EDT 2003


[Jeremy Fincher]

> The topic really says it all -- I'd like to *force* repr(aString) to
> use double quotes.  Is that possible?  If it's not, then is it possible
> to achieve the same thing (everything escaped properly, etc.) through
> other means?

Pymacs once needed exactly this, from the that double quoted strings
in Python and in Emacs were "compatible".  But after Emacs changed how
hexadecimal escapes are represented, the trick did not work anymore,
and I had to abandon it for something more explicit.

Let me seek in obsolete Pymacs releases...  [dig, dig, dig...]  OK,
found it!  The kludge below was found with the kind collaboration of the
Python community, mixing the ideas that were discussed at the time.


# 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:])

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





More information about the Python-list mailing list