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

Ron Adam ron3200 at gmail.com
Sat Aug 8 15:09:50 CEST 2015



On 08/08/2015 12:56 AM, Andrew Barnert via Python-ideas wrote:
> On Aug 7, 2015, at 18:52, Ron
> Adam<ron3200 at gmail.com>  wrote:
>>>
>>>>> 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.

> Which most languages? In C, sh, perl, and most of their respective
> descendants, it means x. (Perl also goes out of its way to guarantee
> that if x is a punctuation character, it will never mean anything but x
> in any future version, either in strings or in regexps, so it's always
> safe to unnecessarily escape punctuation instead of remembering the
> rules for what punctuation to escape.)

Actually this is what I thought, but when looking up what other languages 
do in this case, it was either not documented or suggested it raised an 
error.  Apparently in C, it is suppose to raise an error, but compilers 
have supported echoing the escaped character instead.


 From https://en.wikipedia.org/wiki/Escape_sequences_in_C
--------------------
Non-standard escape sequences

A sequence such as \z is not a valid escape sequence according to the C 
standard as it is not found in the table above. The C standard requires 
such "invalid" escape sequences to be diagnosed (i.e., the compiler must 
print an error message). Notwithstanding this fact, some compilers may 
define additional escape sequences, with implementation-defined semantics. 
An example is the \e escape sequence, which has 1B as the hexadecimal value 
in ASCII, represents the escape character, and is supported in GCC,[1] 
clang and tcc.
---------------------


> The only language I can think of off the top my head that raises an
> error is Haskell.

> I like the Haskell behavior better than the C/perl behavior, especially
> given the backward compatibility issues with Python up to 3.5 if it
> switched, but I don't think it's what most languages do.

I like the Haskell behaviour as well.

Cheers,
     Ron




More information about the Python-ideas mailing list