Re: Unrecognized escape sequences in string literals



On Aug 9, 11:10 pm, Steven D'Aprano
<ste...@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
On Sun, 09 Aug 2009 18:34:14 -0700, Carl Banks wrote:
Why should a backslash in a string literal be an error?

Because the behavior of \ in a string is context-dependent, which means
a reader can't know if \ is a literal character or escape character
without knowing the context, and it means an innocuous change in context
can cause a rather significant change in \.

*Any* change in context is significant with escapes.

"this \nhas two lines"

If you change the \n to a \t you get a significant difference. If you
change the \n to a \y you get a significant difference. Why is the first
one acceptable but the second not?

Because when you change \n to \t, you've haven't changed the meaning
of the \ character; but when you change \n to \y, you have, and you
did so without even touching the backslash.


IOW it's an error-prone mess.

I've never had any errors caused by this.

Thank you for your anecdotal evidence. Here's mine: This has gotten
me at least twice, and a compiler complaint would have reduced my bug-
hunting time from tens of minutes to ones of seconds. [Aside: it was
when I was using Python on Windows for the first time]


I've never seen anyone write to
this newsgroup confused over escape behaviour, or asking for help with an
error caused by it, and until this thread, never seen anyone complain
about it either.

More anecdotal evidence. Here's mine: I have.


Excuse my cynicism, but I believe that you are using "error-prone" to
mean "I don't like this behaviour" rather than "it causes lots of errors"..

No, I'm using error-prone to mean error-prone.

Someone (obviously not you because you're have perfect knowledge of
the language and 100% situation awareness at all times) might have a
string like "abcd\stuv" and change it to "abcd\tuvw" without even
thinking about the fact that the s comes after the backslash.

Worst of all: they might not even notice the error, because the repr
of this string is:

'abcd\tuwv'

They might not notice that the backslash is single, because (unlike
you) mortal fallible human beings don't always register tiny details
like a backslash being single when it should be double.

Point is, this is a very bad inconsistency. It makes the behavior of
\ impossible to learn by analogy, now you have to memorize a list of
situations where it behaves one way or another.


Carl Banks
.



Relevant Pages

  • Re: Unrecognized escape sequences in string literals
    ... character without knowing the context, ... '\n' maps to the string chr. ... and thinks "it's an escape sequence that returns the bytes '\y'". ...
    (comp.lang.python)
  • Re: Unrecognized escape sequences in string literals
    ... how unrecognized escape sequences are treated in Python. ... a backslash is just an ordinary character, ... Why should a backslash in a string literal be an error? ...
    (comp.lang.python)
  • Re: Getting Starting in JavaScript et al
    ... To date I have had three epiphanies where I have suddenly "got" javascript and realized how utterly diffident my previous understanding of the subject had been. ... Identifying and understanding the types of values that javascript uses; that there are 5 primitive data types (boolean, number, string, null and the undefined type). ... tab would then contain the tab character, ... Javascript has escape sequences that start with a backslash, but there are a number of types. ...
    (comp.lang.javascript)
  • Re: escaped identifier vs regular names ?
    ... Escaped identifiers are not in a separate namespace from normal ... the terminating whitespace character to be a space character, ... And, yes, many Verilog tools create escaped names of this form. ... escape names, it isn't really handling the entire Verilog language. ...
    (comp.lang.verilog)
  • Re: Unrecognized escape sequences in string literals
    ... character without knowing the context, ... '\n' maps to the string chr. ... In both cases the backslash in the literal have the same meaning: ... escape every backslash, ...
    (comp.lang.python)