变量分配给最后执行的行? [英] Variable assigned to the last executed line?
问题描述
我刚刚在Python解释器中发现了一些奇怪的东西.让我告诉你:
I just found something weird in the Python interpreter. Let me show you:
$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> _
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> 5 + 4
9
>>> _
9
>>> 'Hello world'
'Hello world'
>>> _
'Hello world'
>>> type(3.5)
<type 'float'>
>>> _
<type 'float'>
您可以在口译员中尝试此操作;这里没有技巧在工作!
You can try this in your interpreter; there are no tricks at work here!
最后执行的行的结果是否分配给名为_
的变量?
Is the result of the last executed line being assigned to a variable named _
?
有人知道吗?是否有任何相关文档?在什么情况下有用?
Does anybody know something about it? Is there any documentation about it? In which situation could it be useful?
推荐答案
在这里看看保留的标识符python .
特殊标识符_在交互式解释器中用于 存储上一次评估的结果;它存储在 内置模块.
The special identifier _ is used in the interactive interpreter to store the result of the last evaluation; it is stored in the builtin module.
此行为也可以在haskell的交互式环境ghci
中找到.在这里使用it
代替_
.
This behavior can be found on haskell's interactive environment ghci
also. Here instead of _
use it
.
Prelude> 2+2
4
Prelude> it
4
这篇关于变量分配给最后执行的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!