为什么PyCharm使用双反斜杠表示转义? [英] Why does PyCharm use double backslash to indicate escaping?

查看:2027
本文介绍了为什么PyCharm使用双反斜杠表示转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我写了一个正常的字符串和另一个异常字符串,如下所示:





现在我调试它,发现在调试工具中,异常字符串将显示如下:





以下是问题:



为什么PyCharm显示双反斜杠而不是单反斜杠?众所周知, \'表示'。有没有什么伎俩?

解决方案

我认为发生的是' c 变量字符串需要转义,PyCharm在运行时知道这一点,因为您已经在(你会在调试器中注意到,你的 c 字符串现在被'包围)。单引号将其更改为 \',但现在,您的字符串中需要转义的 \ ,并在Python中转义 \ ,您键入 \\



编辑
让我看看我是否可以解释这里的转义顺序。


  1. 你这不正常被分配到 c

  2. PyCharm将 c 中的字符串转换为'u'这在运行时不正常'如何,没有逃避第二个',你的字符串是n在 u 之后关闭。

  3. PyCharm自动转义'你之前添加一个斜杠。该字符串现在是'你'这不正常'。在这一点上,一切都应该是好的,但PyCharm可能会采取额外的步骤安全。

  4. 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.

  1. "u' this is not normal" is assigned to c
  2. 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 after u.
  3. 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.
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆