1

I have run a function, f(), which took just over 10 hours to complete. Unfortunately I made the mistake of not assigning its output, i.e., foo = f().

I have also changed the value of the underscore, '_', variable.

I am wondering, are the results of f() still in memory? Is there any way to access them?

emac
  • 35
  • 1
  • 3
  • 2
    See [this answer](http://stackoverflow.com/a/200045/2781698). This only works if you are in an interactive shell (such as iPython) – Mathias711 May 10 '16 at 08:06
  • That's a great answer thank you but unfortunately it is not available on the interactive shell that I am using :-( – emac May 10 '16 at 08:12
  • @emac: then you are using the regular Python interactive shell, not IPython (which is what that answer applies to). – Martijn Pieters May 10 '16 at 08:35

1 Answers1

0

I'm assuming you are in the Python interactive interpreter, and that you haven't actually used _ to echo it's value! If you did, then the output of your function call is lost.

If you did not echo anything else, but bound _ before, then that's a new global shadowing the interactive interpreter built-in that captured your function output.

Use del _ to remove it and reveal the built-in, that still will hold your function output:

>>> del _
>>> output = _

This works because the built-in _ name is always bound to the last expression result regardless of there being a global _:

>>> _ = 'foo'
>>> 1 + 1  # an expensive operation!
2
>>> del _
>>> _
2

If you are using IPython, then you can access all results by their output number; if a result is echoed with Out[42] in front, then use _42 to access the cached result.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Unfortunately I echoed the time taken to run the program! Looks like the results are gone! You live and learn.... – emac May 10 '16 at 09:20
  • @emac: very sorry to hear that! Perhaps you want to switch to IPython anyway, if you are doing a lot of data processing or exploration in Python that interactive shell has a lot of powerful features to assist you. Like caching of recent results. – Martijn Pieters May 10 '16 at 09:21