Unrecognized escape sequences in string literals

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Aug 10 10:49:20 CEST 2009


On Mon, 10 Aug 2009 00:57:18 -0700, Douglas Alan wrote:

> On Aug 10, 2:10 am, Steven D'Aprano
> 
>> I've never had any errors caused by this.
> 
> But you've seen an error caused by this, in this very discussion. I.e.,
> "foo\xbar".


Your complaint is that "invalid" escapes like \y resolve to a literal 
backslash-y instead of raising an error. But \xbar doesn't contain an 
invalid escape, it contains a valid hex escape. Your ignorance that \xHH 
is a valid hex escape (for suitable hex digits) isn't an example of an 
error caused by "invalid" escapes like \y.



> "\xba" isn't an escape sequence in any other language that I've used,
> which is one reason I made this error... Oh, wait a minute -- it *is* an
> escape sequence in JavaScript. But in JavaScript, while "\xba" is a
> special character, "\xb" is synonymous with "xb".
> 
> The fact that every language seems to treat these things similarly but
> differently, is yet another reason why they should just be treated
> utterly consistently by all of the languages: I.e., escape sequences
> that don't have a special meaning should be an error!

Perhaps all the other languages should follow Python's lead instead?

Or perhaps they should follow bash's lead, and map \C to C for every 
character. If there were no special escapes at all, Windows programmers 
wouldn't keep getting burnt when they write "C:\\Documents\today\foo" and 
end up with something completely unexpected.

Oh wait, no, that still wouldn't work, because they'd end up with 
C:\Documentstodayfoo. So copying bash doesn't work.

But copying C will upset the bash coders, because they'll write 
"some\ file\ with\ spaces" and suddenly their code won't even compile!!!

Seems like no matter what you do, you're going to upset *somebody*.



>> I've never seen anyone write to
>> this newsgroup confused over escape behaviour,
> 
> My friend objects strongly the claim that he is "confused" by it, so I
> guess you are right that no one is confused. He just thinks that it
> violates the beautiful sense of aesthetics that he was sworn over and
> over again Python to have.

Fair enough.



-- 
Steven



More information about the Python-list mailing list