Forcing quote characters in repr(STRING), how?
Tim Peters
tim.one at home.com
Wed May 2 18:04:24 EDT 2001
[François Pinard]
> When using `repr()' on a string, Python automatically selects if it
> will use single or double quotes to enclose the produced
> representation of the string. Is there a clean way to force that
> choice to be, under user control, a particular quote character
> (either simple or double)?
Not a clean way, nor even a dirty way: the logic is hardcoded.
/* figure out which quote to use; single is preferred */
quote = '\'';
if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
quote = '"';
IOW,
quote = "'"
if "'" in string and '"' not in string:
quote = '"'
The cleanest way would be to wrap your strings in a subclass of UserString,
and override __repr__ in the subclass.
More information about the Python-list
mailing list