为什么PyCharm使用双反斜杠表示转义? [英] Why does PyCharm use double backslash to indicate escaping?
问题描述
现在我调试它,发现在调试工具中,异常字符串将显示如下:
以下是问题:
为什么PyCharm显示双反斜杠而不是单反斜杠?众所周知, \'
表示'
。有没有什么伎俩?
我认为发生的是'
c
变量字符串需要转义,PyCharm在运行时知道这一点,因为您已经在$ c $中包含了完整的字符串c>(你会在调试器中注意到,你的
c
字符串现在被'
包围)。单引号将其更改为 \'
,但现在,您的字符串中需要转义的 \
,并在Python中转义 \
,您键入 \\
。
编辑
让我看看我是否可以解释这里的转义顺序。
-
你这不正常
被分配到c
- PyCharm将
c
中的字符串转换为'u'这在运行时不正常'
如何,没有逃避第二个'
,你的字符串是n在u
之后关闭。 - PyCharm自动转义
'
你之前添加一个斜杠。该字符串现在是'你'这不正常'
。在这一点上,一切都应该是好的,但PyCharm可能会采取额外的步骤安全。 - PyCharm然后转义刚刚添加到字符串的斜线,将字符串留作:
'你'不正常'
。
很可能设置PyCharm。是否导致您的代码实际出现问题?
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? 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 \\
.
EDIT Let me see if I can explain the order of escaping going on here.
"u' this is not normal"
is assigned toc
- PyCharm converts the string in
c
to'u' this is not normal'
at runtime. See how, without escaping the 2nd'
, your string is now closed off right afteru
. - PyCharm escapes the
'
automatically for you by adding a slash before it. The string is now'u\' this is not normal'
. At this point, everything should be fine but PyCharm may be taking an additional step for safety. - PyCharm then escapes the slash it just added to your string, leaving the string as:
'u\\' this is not normal'
.
It is likely a setting inside PyCharm. Does it cause an actual issue with your code?
这篇关于为什么PyCharm使用双反斜杠表示转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!