6

I am working on a script and have accidentally deleted the code of a function a while back (so cant Undo) but the function is saved in the memory.

print(diff)

#output
<function diff at 0x7f48f238a670>

Is there a way to print this function's code?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Does this answer your question? [How can I get the source code of a Python function?](https://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function) – mkrieger1 Oct 16 '20 at 16:38
  • Does this answer your question? [How do you get Python to write down the code of a function it has in memory?](https://stackoverflow.com/questions/399991/how-do-you-get-python-to-write-down-the-code-of-a-function-it-has-in-memory) – Murtaza Haji Oct 16 '20 at 16:41
  • 3
    @MurtazaHaji: That relies on the source file still existing. – user2357112 Oct 16 '20 at 16:41
  • Not if the source code is gone. – martineau Oct 16 '20 at 17:07
  • 1
    I would start with retrieving the bytecode using [dis](https://docs.python.org/3/library/dis.html) and then try to convert back to python using [uncompyle6](https://pypi.org/project/uncompyle6/) or similar, depending on the actual python version. – awesoon Oct 18 '20 at 07:22
  • This might help maybe? https://stackoverflow.com/q/8250625/12134794 – bot-coder101 Jun 21 '21 at 22:36

0 Answers0