Say I ran an expensive operation foo() which returns me a large list, but I forgot to save the output of foo() inside a variable.
Let's assume if I run foo() again, I will get a different output.
But I really need the output from the first time I ran foo().
In effect, I am asking if there is some buffer that stores the output of the last command, which I could read?
_
(single underscore) works for me in python 3 for windows, should work for other versions as well:
>>> 1 + 1
2
>>> x = _
>>> x
2
The _
character (single underscore) is defined as the output of the last evaluation in all versions of Python, but only in the interactive shell. See: the docs
Example:
>>> def foo():
>>> return 3
>>> foo()
3
>>> _ + 1
4
Based on your question, it sounds like you only care about how to do this in an interactive shell; for the interest of completeness the above functionality is not defined for the non-interactive shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With