On Mon, Mar 16, 2020 at 02:36:25PM +0900, Stephen J. Turnbull wrote:
> Kyle Stanley writes:
>
> > The behavior is the same on Python 3.8.2:
> >
> > Python 3.8.2 (default, Feb 26 2020, 22:21:03)
> > [GCC 9.2.1 20200130] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> r'x\'y'
> > "x\\'y"
>
> This looks like a defect to me. The "'" *is* being quoted.
If it's a defect, it's one which goes all the way back to Python 1.5:
[steve@ando ~]$ python1.5
>>> r'x\'y'
"x\\'y"
and is fully documented :-)
[quote]
Even in a raw literal, quotes can be escaped with a backslash, but the
backslash remains in the result; for example, r"\"" is a valid string
literal consisting of two characters: a backslash and a double quote;
r"\" is not a valid string literal (even a raw string cannot end in an
odd number of backslashes).
[/quote]
https://docs.python.org/3/reference/lexical_analysis.html#literals
> > > > Raw strings aren't quite fully raw, which is why you can't use raw
> > > > strings for Windows paths:
> > > > path = r'somewhere\some\folder\'
> > > > doesn't work. The reason is that "raw" (semi-cooked?) strings are
> > > > s/are/were/ intended for regexes[.]
>
> With all due respect to Steve d'A, I think that reason is inaccurate
> (at least in MacPorts' Python 3.8.2). I get
>
> >>> path = r'somewhere\some\folder\'
> File "<stdin>", line 1
> path = r'somewhere\some\folder\'
> ^
> SyntaxError: EOL while scanning string literal
>
> The reason for that, I believe, is that the rightmost "'" is quoted,
> and there is no "'" terminating the string literal.
Yes, that's what I meant when I said "it doesn't work". And the reason
it doesn't work, as I understand it, is that raw strings were designed
for use in regular expressions. Regexes cannot end in an odd number of
backslashes.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/55MHFCQNQGSKW7YZDYUZNPOFZI5XFBG5/
Code of Conduct: http://python.org/psf/codeofconduct/