Why can't Python's raw string literals end with a single backslash?

后端 未结 12 1201
后悔当初
后悔当初 2020-11-22 07:37

Technically, any odd number of backslashes, as described in the documentation.

>>> r\'\\\'
  File \"\", line 1
    r\'\\\'
       ^
Syn         


        
12条回答
  •  失恋的感觉
    2020-11-22 07:59

    Another user who has since deleted their answer (not sure if they'd like to be credited) suggested that the Python language designers may be able to simplify the parser design by using the same parsing rules and expanding escaped characters to raw form as an afterthought (if the literal was marked as raw).

    I thought it was an interesting idea and am including it as community wiki for posterity.

提交回复
热议问题