Unrecognized escape sequences in string literals

Douglas Alan darkwater42 at gmail.com
Sun Aug 9 21:26:54 CEST 2009


A friend of mine is just learning Python, and he's a bit tweaked about
how unrecognized escape sequences are treated in Python. This is from
the Python 3.0 reference manual:

     Unlike Standard C, all unrecognized escape sequences are left in
the string unchanged, i.e.,
     the backslash is left in the string. (This behavior is useful
when debugging: if an escape
     sequence is mistyped, the resulting output is more easily
recognized as broken.) It is also
     important to note that the escape sequences only recognized in
string literals fall into the
     category of unrecognized escapes for bytes literals.

My friend begs to differ with the above. It would be much better for
debugging if Python generated a parsing error for unrecognized escape
sequences, rather than leaving them unchanged. g++ outputs a warning
for such escape sequences, for instance. This is what I would consider
to be the correct behavior. (Actually, I think it should just generate
a fatal parsing error, but a warning is okay too.)

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?

|>ouglas



More information about the Python-list mailing list