![](/img/trans.png)
[英]When in Python interactive shell, can I copy the previous command to the clipboard?
[英]How do I retrieve the output of a previous command and save it in a variable inside the Python interactive shell?
假设我运行了一个昂贵的操作foo(),它返回一个大的列表,但我忘了将foo()的输出保存在变量中。
假设我再次运行foo(),我会得到一个不同的输出。
但是我第一次运行foo()时确实需要输出。
实际上,我问是否有一些缓冲区存储了最后一个命令的输出,我可以读取?
_
(单下划线)适用于我的python 3 for windows,也适用于其他版本:
>>> 1 + 1
2
>>> x = _
>>> x
2
_
字符(单个下划线)被定义为所有Python版本中最后一次评估的输出,但仅限于交互式shell中。 请参阅: 文档
例:
>>> def foo():
>>> return 3
>>> foo()
3
>>> _ + 1
4
根据您的问题,听起来您只关心如何在交互式shell中执行此操作; 为了完整性的利益,没有为非交互式shell定义上述功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.