[Python-ideas] Make non-meaningful backslashes illegal in string literals

Ron Adam ron3200 at gmail.com
Sat Aug 8 03:52:52 CEST 2015



On 08/06/2015 12:26 PM, random832 at fastmail.us 
wrote:
> On Wed, Aug 5, 2015, at 14:56, Eric V. Smith wrote:
>> >Because strings containing \{ are currently valid
> Which raises the question of why. (and as long as we're talking about
> things to deprecate in string literals, how about \v?)

(In the below consider x as any character.)

In most languages if \x is not a valid escape character, then an error is 
raised.

In regular expressions when \x is not a valid escape character, they just 
makes it x.

    \s ---> s
    \h ---> h

In Python it's \ + x.

    \s  --> \\s
    \h  --> \\h

Personally I think if \x is not a valid escape character it should raise an 
error.  But since it's a major change in python, I think it would need to 
be done in a major release, possibly python4.

Currently if a new escape characters needs to be added, it involve the risk 
of breaking currently working code.  It can be handled but it's not what I 
think is the best approach.  It would be better if we could make escape 
codes work only if they are valid, and raise an error if they are not. 
Then when/if any new escape codes are added, it's not as much of a 
backwards compatible problem.  That means '\ ' would raise an error, and 
would need to be '\\ ' or r'\ '.

But we probably need to wait until a major release to do this.  I'd be for 
it, but I understand why a lot of people would not like it.  It would mean 
they may need to go back and repair possibly a lot(?) of code they already 
written.  It's not pleasant to have a customers upset when programs break.

Cheers,
    Ron



More information about the Python-ideas mailing list