Simple question - end a raw string with a single backslash ?
Antoon Pardon
antoon.pardon at vub.be
Mon Oct 19 04:25:20 EDT 2020
Op 13/10/20 om 15:14 schreef Serhiy Storchaka:
> 13.10.20 11:52, Tony Flury via Python-list пише:
>> I am trying to write a simple expression to build a raw string that ends
>> in a single backslash. My understanding is that a raw string should
>> ignore attempts at escaping characters but I get this :
>>
>> >>> a = r'end\'
>> File "<stdin>", line 1
>> a = r'end\'
>> ^
>> SyntaxError: EOL while scanning string literal
>>
>> I interpret this as meaning that the \' is actually being interpreted as
>> a literal quote - is that a bug ?
>
> r'You can\'t end raw string literal with a single "\"'
>
> If backslash be true inner in a raw string, the above literal would end
> after \'. It would be very hard to write a raw string containing both \'
> and \", and even \''' and \""" (there are such strings in the stdlib).
>
> So you have problem either with trailing backslash, or with inner
> backslash followed by quotes. Both problems cannot be solved at the same
> time. Python parser works as it works because initially it was easier to
> implement, and now this cannot be changed because it would break some
> amount of correct code.
IMO the way python does this is broken.
>>> st=r'You can\'t end raw string literal with a single "\"'
>>> print(st)
Now either the \ is special or it is not.
1) If it is special, it should change how the ' is treated but not
appear itself.
2) If it is not special, it should just appear and not change how the '
is treated.
What python does here is a combination of both. The \ appears and it
changes how the ' is treated. That is IMO broken.
--
Antoon Pardon.
More information about the Python-list
mailing list