Skip to main content
 首页 » 编程设计

python之分配给最后执行的行的变量

2025年05月04日14bonelee

<分区>

我刚刚在 Python 解释器中发现了一些奇怪的东西。让我告诉你:

$ 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'> 

你可以在你的解释器中试试这个;这里没有任何技巧!

是否将最后执行的行的结果分配给名为_的变量?

有人知道吗?有没有关于它的文件?它在什么情况下有用?