In Python is there a history of the function that i ran?
Question: After writing for a long time tons of stuff i've deleted a certain function...
CTRL + y couldn't save me. basically i had:

    
    
    def foo(todo):
        print 'how why where'
    

in a .py file, i have deleted function and its is not traceable from the file.
Since ipython ran the function, and interpreter is still live, is there a way
to watch the history of the interpreter running the function? I managed to
find the function name by the command:

    
    
    history
    

also from [Export Python interpreter history to a
file?](http://stackoverflow.com/questions/8421097/export-python-interpreter-
history-to-a-file):

    
    
    import atexit
    import os
    import readline
    import rlcompleter
    
    historyPath = os.path.expanduser("~/.pyhistory")
    
    def save_history(historyPath=historyPath):
        import readline
        readline.write_history_file(historyPath)
    
    if os.path.exists(historyPath):
        readline.read_history_file(historyPath)
    
    atexit.register(save_history)
    del os, atexit, readline, rlcompleter, save_history, historyPath
    

but it also the same as plain history The real question is where is the
history, or trace, from which python can relaunch a function even if the file
was deleted as long as the session of the interpreter is alive. I can run the
function again & again, because it is complied somewhere.

Thanks!


Answer: All you have available at this point is the compiled code. You can disassemble
that into a readable byte code listing, and that may help you to reconstruct
the original source. It will be hard work, because the byte codes are quite
low level, but it may be better than nothing.

    
    
    import dis
    dis.disassemble(foo.__code__)
    

The result looks like this:

    
    
      2           0 LOAD_CONST               1 ('how why where')
                  3 PRINT_ITEM          
                  4 PRINT_NEWLINE       
                  5 LOAD_CONST               0 (None)
                  8 RETURN_VALUE        
    

