为什么在Python解释器中键入_会返回True? [英] Why does typing _ in the Python interpreter return True?
问题描述
我的口译员行为很奇怪:
I am getting very weird interpreter behaviour:
>>> _
True
>>> type(True)
<class 'bool'>
>>> type(_)
<class 'bool'>
我之所以这样做是因为_是Bpython中的一个建议,但它似乎也可以在普通解释器中使用.我正在使用
I tried this because _ came up as a suggestion in Bpython, but it seems to work in the normal interpreter too. I am using
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
有人可以解释_为什么用_代替True吗?是否是旧式的,类似于;"可以用来结束命令,但这不是必需/不鼓励的吗?
Can anybody explain why _ is a substitute for True? Is it legacy, similarly to how ";" can be used to end commands, but is not necessary/encouraged?
这似乎是随机的.在新的终端中不会发生这种情况,但是一旦我开始从事某件事,_就开始成为现实.到底是怎么回事?
It seems to be random. This does not happen in a new terminal, but once I start working on something _ starts becoming true. What the hell is going on?
推荐答案
_
将是最后计算的命令的结果-解释器启动时没有任何内容,因此您会得到NameError
. .之后,您将得到以前的结果...尝试打开一个新的解释器并执行2 + 2
...,您将看到返回的4
,然后键入_
...,例如:
_
will be the result of the last evaluated command - at interpreter start up there isn't any so you'll get a NameError
... after that, you'll get the previous result... Try opening a new interpreter and doing 2 + 2
... you'll see 4
returned, then type _
... eg:
>>> _
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
_
NameError: name '_' is not defined
>>> 2 + 2
4
>>> _
4
这篇关于为什么在Python解释器中键入_会返回True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!