Python Literal r'\' Not Accepted

前端 未结 5 2106
感情败类
感情败类 2021-01-17 08:25

r\'\\\' in Python does not work as expected. Instead of returning a string with one character (a backslash) in it, it raises a SyntaxError. r\"\\\"

5条回答
  •  难免孤独
    2021-01-17 08:56

    The answer to my question ("Why is a backslash not allowed as last character in raw strings?") actually to me seems to be "That's a design decision", furthermore a questionable one.

    Some answers tried to reason that the lexer and some syntax highlighters are simpler this way. I don't agree (and I have some background on writing parsers and compiler as well as IDE development). It would be simpler to define raw strings with the semantics that a backslash has no special meaning whatsoever. Both lexer and IDE would benefit from this simplification.

    The current situation also is a wart: In case I want a quote in a raw string, I cannot use this anyway. I only can use it if I happen to want a backslash followed by a quote inside my raw string.

    I would propose to change this, but I also see the problem of breaking existing code :-/

提交回复
热议问题