Raw String Question
Jim Garrison
jgarrison at troux.com
Thu Mar 12 15:28:50 EDT 2009
Tim Chase wrote:
>> >>> r"a\"
>> SyntaxError: EOL while scanning string literal (<pyshell#45>, line 1)
>>
>> It seems the parser is interpreting the backslash as an escape
>> character in a raw string if the backslash is the last character.
>> Is this expected?
>
> Yep...as documented[1], "even a raw string cannot end in an odd number
> of backslashes".
>
> -tkc
>
>
> [1]
> http://docs.python.org/reference/lexical_analysis.html
OK, I'm curious as to the reasoning behind saying that
When an 'r' or 'R' prefix is present, a character following a
backslash is included in the string without change, and all
backslashes are left in the string.
which sounds reasonable, but then saying in effect "Oh wait, let's
introduce a special case and make it impossible to have a literal
backslash as the last character of a string without doubling it".
So you have a construct (r'...') whose sole reason for existence
is to ignore escapes, but it REQUIRES an escape mechanism for one
specific case (which comes up frequently in Windows pathnames).
I would suggest that this is pathologically inconsistent (now donning
my flameproof underwear "-)
At the very least the "all backslashes are left in the string" quote
from the Lexical Analysis page (rendered in italics no less) needs to
be reworded to include the exception instead of burying this in a
parenthetical side-comment.
More information about the Python-list
mailing list