Re: Unrecognized escape sequences in string literals



On Aug 9, 5:06 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
On Sun, 09 Aug 2009 12:26:54 -0700, Douglas Alan wrote:
A friend of mine is just learning Python, and he's a bit tweaked about
how unrecognized escape sequences are treated in Python.
...
In any case, I think my friend should mellow out a bit, but we both
consider this something of a wart. He's just more wart-phobic than I am..
Is there any way that this behavior can be considered anything other
than a wart? Other than the unconvincing claim that you can use this
"feature" to save you a bit of typing sometimes when you actually want a
backslash to be in your string?

I'd put it this way: a backslash is just an ordinary character, except
when it needs to be special. So Python's behaviour is "treat backslash as
a normal character, except for these exceptions" while the behaviour your
friend wants is "treat a backslash as an error, except for these
exceptions".

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 \.

IOW it's an error-prone mess. It would be better if Python (like C)
treated \ consistently as an escape character. (And in raw strings,
consistently as a literal.)

It's kind of a minor issue in terms of overall real-world importance,
but in terms of raw unPythonicness this might be the worst offense the
language makes.


Carl Banks
.



Relevant Pages

  • 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)
  • Re: gfortran diagnostics and so on
    ... Well, in f0003, backslash is part of the standard Fortran character set. ... Because the backslash is part of the standard Fortran character set, the default behavior should be the printable character, **NOT** some kind of magic introductory character that transforms the interpretation of following character. ... The one I like best is to use one of the popular extensions to designate a particular literal string according to the C language. ...
    (comp.lang.fortran)
  • Re: Convert to /
    ... |> Why is it so hard to convert backslashes to forward slashes in java? ... | character immediately after the colon would be the tab ... | get a backslash character into a string literal in Java ...
    (comp.lang.java.help)
  • Re: Unrecognized escape sequences in string literals
    ... 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 ... thinking about the fact that the s comes after the backslash. ...
    (comp.lang.python)
  • Re: Raw String Question
    ... It seems the parser is interpreting the backslash as an escape ... character in a raw string if the backslash is the last character. ...
    (comp.lang.python)

Loading