[Python-3000] Raw strings containing \u or \U
Guido van Rossum
guido at python.org
Wed May 16 19:05:45 CEST 2007
On 5/16/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> On 5/16/07, Guido van Rossum <guido at python.org> wrote:
> > Walter Doerwald, in private mail, reminded me of a third use case for
> > raw strings: docstrings containing example code using backslashes.
> > Here it really seems wrong to interpolate \u and \U.
> > So this is swaying me towards changing this behavior: r"\u1234" will
> > be a string of length 6, and r"\U00012345" one of length 10.
> +1 for making raw strings truly raw (where backslashes don't escape
> anything) and teaching the re module about the necessary escapes (\u,
> \n, \r, etc.).
It already knows about all of those except \u and \U. Someone care to
submit a patch?
> > I'm still on the fence about the trailing backslash; I personally
> > prefer to write Windows paths using regular strings and doubled
> > backslashes.
> +1 for no escaping of quotes in raw strings. Python provides so many
> different ways to quote a string, the cases in which you can't just
> switch to another quoting style are vanishingly small. Examples from
> the stdlib and their translations::
> '\'' --> "'"
> '("|\')' --> '''("|')'''
> 'Can\'t stat' --> "Can't stat"
> '(\'[^\']*\'|"[^"]*")?' --> '''('[^']*'|"[^"]*")?'''
> Note that allowing trailing backslashes could also clean up stuff in
> modules like ntpath::
> path[-1] in "/\\" --> path[-1] in r"/\"
> firstTwo == '\\\\' --> firstTwo == r'\\'
Can you also search for how often this feature is *used* (i.e. a raw
string that has to be raw for other reasons also contains an escaped
quote)? If that's rare or we can agree on easy fixes it would ease my
mind about this part of the proposal.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000