简体   繁体   English

为什么PyCharm使用双反斜杠来表示逃逸?

[英]Why does PyCharm use double backslash to indicate escaping?

For instance, I write a normal string and another "abnormal" string like this: 例如,我写了一个普通字符串和另一个“异常”字符串,如下所示:

在此输入图像描述

Now I debug it, finding that in the debug tool, the "abnormal" string will be shown like this: 现在我调试它,发现在调试工具中,“异常”字符串将显示如下:

在此输入图像描述

Here's the question: 这是问题:

Why does PyCharm show double backslashes instead of a single backslash? 为什么PyCharm显示双反斜杠而不是单反斜杠? As is known to all, \\' means ' . 众所周知, \\'意味着' Is there any trick? 有什么伎俩吗?

What I believe is happening is the ' in your c variable string needs to be escaped and PyCharm knows this at runtime, given you have surrounded the full string in " (You'll notice in the debugger, your c string is now surrounded by ' ). To escape the single quote it changes it to \\' , but now, there is a \\ in your string that needs escaping, and to escape \\ in Python, you type \\\\ . 我相信正在发生的是'你的c变量字符串需要被转义并且PyCharm在运行时知道这一点,因为你已经包围了完整的字符串" (你会在调试器中注意到,你的c字符串现在被'包围' )。为了逃避它改变它的单引号\\' ,但现在,有一个\\在字符串中需要转义,并逃避\\ Python中,键入\\\\

EDIT Let me see if I can explain the order of escaping going on here. 编辑让我看看我是否可以解释在这里逃避的顺序。

  1. "u' this is not normal" is assigned to c "u' this is not normal"被分配给c
  2. PyCharm converts the string in c to 'u' this is not normal' at runtime. PyCharm在运行时将c的字符串转换为'u' this is not normal' See how, without escaping the 2nd ' , your string is now closed off right after u . 看看如何在没有逃离第二个' ,你的字符串现在在u之后立即关闭。
  3. PyCharm escapes the ' automatically for you by adding a slash before it. PyCharm逃脱'通过之前增加一个斜杠自动为您。 The string is now 'u\\' this is not normal' . 字符串现在是'u\\' this is not normal' At this point, everything should be fine but PyCharm may be taking an additional step for safety. 在这一点上,一切都应该没问题,但PyCharm可能会采取额外措施来保证安全。
  4. PyCharm then escapes the slash it just added to your string, leaving the string as: 'u\\\\' this is not normal' . PyCharm然后转义它刚刚添加到你的字符串中的斜杠,将字符串保留为: 'u\\\\' this is not normal'

It is likely a setting inside PyCharm. 它可能是PyCharm中的一个环境。 Does it cause an actual issue with your code? 它是否会导致代码出现实际问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM