In many symbolic math systems, such as Matlab or Mathematica, you can use a variable like Ans
or %
to retrieve the last computed value. Is there a similar facility in the Python shell?
views:
481answers:
2It only works in the interactive shell, though. Don't rely on it for scripts.
John Fouhy
2008-10-14 04:54:50
Cool, that works in Ruby's `irb` shell as well.
bk1e
2008-10-14 06:01:00
I never knew this but wondered about it for some time. Thanks.
crystalattice
2008-10-14 07:56:59
+14
A:
Just for the record, ipython takes this one step further and you can access every result with _ and its numeric value
In [1]: 10
Out[1]: 10
In [2]: 32
Out[2]: 32
In [3]: _
Out[3]: 32
In [4]: _1
Out[4]: 10
In [5]: _2
Out[5]: 32
In [6]: _1 + _2
Out[6]: 42
In [7]: _6
Out[7]: 42
And it is possible to edit ranges of lines with the %ed macro too:
In [1]: def foo():
...: print "bar"
...:
...:
In [2]: foo()
bar
In [3]: %ed 1-2
Peter Hoffmann
2008-10-14 04:53:38
Nice! I'll be sure to keep that in mind if I ever get annoyed at Python's shell (which is, admittedly, a long way off, since I'm used to PHP's tardtastic interactive mode).
Edward Z. Yang
2008-10-14 04:59:36