Python 中下划线(“_”)的用途是什么?
我在一些上下文中看到过这一点,例如,
在顺序解包中:
_, x = L.pop() # e.g., L is a list of tuples
初始化容器:
X = _
所以显然这不是正式的 python 语法的元素,而是我所知道的用途似乎是任意的。
所以我很好奇使用它的可能原因是什么以及通常的优点是什么(如果有的话)?
注意:我的问题涉及在脚本、模块等中使用“_”,但不是它在交互式提示中使用。在IDLE中,Python封装的交互式解释器,以及在ipython中,“_”用作最近返回的结果的占位符 >)。
I have seen this in a few contexts, e.g.,
in sequence unpacking:
_, x = L.pop() # e.g., L is a list of tuples
to initialize a container:
X = _
So obviously this is not an element of the formal python syntax, rather the uses i am aware of appear discretionary.
So I'm curious what is the likely reason for its use and what are the advantages generally (if any)?
Note: my question relates to the use of "_" in scripts, modules, etc., but not its use at the interactive prompt. In IDLE, the interactive interpreter packaged with python, as well as in ipython, "_", is used as a placeholder for most recently returned result).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我见过它有两种用途。两者都作为一次性变量,但更常见的是作为国际化的文本包装器。
丢弃变量
虽然我不推荐这样做。称之为“被忽略”。
更清楚了。
i18n 包装器
label
这里将是一个“消息”,一个具有消息 id 和域的对象,当呈现到用户界面(如网页)时,它将被转换为当前用户通过消息目录的语言,以便标签最终以用户的本地语言显示。此处使用
_
是因为它简短且不引人注目。生成的代码_("The label text")
看起来与字符串没有什么不同,而MyDomainMessage("The label text")
看起来确实非常不同而且也更长。I've seen it used in two ways. Both as a throw-away variable but more commonly as a text wrapper for internationalization.
Throw-away variable
Although I would not recommend this. Call it "ignored" instead.
It's clearer.
i18n wrapper
label
will here be a "Message", an object that has a message id and a domain, and when rendered to a user interface (like a web page) it will be translated to the current user language via a message catalog, so that the label will end up in the local language of the user._
is used here because it is short and unobtrusive. The resulting code,_("The label text")
doesn't look to different from just a string, whileMyDomainMessage("The label text")
does look very different and also is much longer._
是一个比较特殊的变量名。在 shell 中,它包含先前计算的表达式的值:在脚本中,它通常用作一次性变量,即出于语义原因需要但稍后不会使用的变量。从语法上讲,它只是一个与其他变量一样的变量。
_
is a somewhat special variable name. In the shell, it contains the value of the previously evaluated expression:Within scripts, it's commonly used as a throwaway variable, i. e. one that's needed for semantical reasons but that isn't going to be used later. Syntactically, it's just a variable like any other.
优点是角色单一,“无名”。
缺点是它经常用于 I18N,作为对
gettext.gettext()
。The advantages are that it's a single character and it's "nameless".
The disadvantage is that it's frequently used for I18N, as a reference to
gettext.gettext()
.我见过当您不关心它会获得什么值时使用变量
_
,或多或少类似于/dev/null
变量。在Python哲学中,可读性比其他语言更有价值,例如:
比其他替代方案更紧凑且更具可读性,表明您不关心结果的z坐标。其他用途可以是
或
对于语言,实际上
_
是与任何其他标识符一样的有效标识符,即使在某些交互式 python 循环中它具有特殊含义。这种用作“匿名变量”的情况并不常见,但仍然存在,因此您可能会在现有代码中找到它。例如 PyChecker 不会报告有关上面的
matrix
函数的任何信息,但如果您使用“正常”变量名称。然而,也有一些软件包为
_
提供了明确定义的含义(gettext)。I've seen the variable
_
used when you don't care about what value it will get, more or less like a/dev/null
variable.In python philosophy readability is much more valued than in other languages, and for example:
is compact and more readable than other alternatives, showing that you don't care about the
z
coordinate of the result. Other uses could beor
For the language indeed
_
is a valid identifier as any other one even if in some interactive python loops it has a special meaning.This use as an "anonymous variable" is not very frequent but still there so you may find it in existing code. For example PyChecker will not report anything about the
matrix
function above but it will complain if you use a "normal" variable name.There are also packages that however give to
_
a well defined meaning (gettext).没有充分的理由在脚本中使用
_
。在交互式解释器中使用时,如果您正在进行任何数学计算,并且想要先前表达式的结果,则可以使用_
。 Python 教程在相同的上下文中引入了_
。我发现该行为是从某些 shell 行为继承而来的。在您的示例中:
会失败,因为 L.pop() 将返回单个值,并且您不能将其分配给两个变量,而且将值分配给具有特殊含义的
_
也不是一个好主意。There is not a good reason to use
_
in the scripts. While using from interactive interpretor if you are doing any mathematical calculation, and you want the the result of the previous expression, you can use_
. Python tutorial introduces the_
in the very same context. I see that the behavior is carried over from certain shell behaviors.In your example:
would fail because L.pop() would return a single value and you cannot assign it to two variables and also it not a good idea to assign values to
_
which has a special meaning.