Reputation: 703
I am working in the Python Interactive Shell (ActiveState ActivePython 2.6.4 under Windows XP). I created a function that does what I want. However, I've cleared the screen so I can't go back and look at the function definition. It is also a multiline function so the up arrow to redisplay lines is of minimal value. Is there anyway to return the actual code of the function? There are "code" and "func_code" attributes displayed by dir(), but I don't know if they contain what I need.
Upvotes: 16
Views: 24926
Reputation: 11
You can try pressing "ctrl+r" (i.e. reverse i search in bash) and then typing
def <name of your function>
and then pressing enter , it might reveal the function definition
Upvotes: 0
Reputation: 7421
It has been a pretty long time but may someone need it. getsource
gives the source code:
>>> def sum(a, b, c):
... # sum of three number
... return a+b+c
...
>>> from dill.source import getsource
>>>
>>> getsource(sum)
'def sum(a, b, c):\n # sum of three number\n return a+b+c\n'
to install dill
run pip install dill
. To get it formatted:
>>> fun = getsource(sum).replace("\t", " ").split("\n")
>>> for line in fun:
... print line
...
def sum(a, b, c):
# sum of three number
return a+b+c
Upvotes: 4
Reputation: 64911
No, not really. You could write yourfunc.func_code.co_code (the actually compiled bytecode) to a file and then try using decompyle or unpyc to decompile them, but both projects are old and unmaintained, and have never supported decompiling very well.
It's infinitely easier to simply write your function to a file to start with.
Upvotes: 1
Reputation: 882851
No, __code__
and func_code
are references to the compiled bytecode -- you can disassemble them (see dis.dis
) but not get back to the Python source code.
Alas, the source code is simply gone, not remembered anywhere...:
>>> import inspect
>>> def f():
... print 'ciao'
...
>>> inspect.getsource(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 694, in getsource
lines, lnum = getsourcelines(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 683, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 531, in findsource
raise IOError('could not get source code')
IOError: could not get source code
>>>
If inspect
can't get to it, that's a pretty indicative sign.
If you were on a platform using GNU readline
(basically, any except Windows), you might exploit the fact that readline
itself does remember some "history" and can write it out to a file...:
>>> readline.write_history_file('/tmp/hist.txt')
and then read that history file -- however, I know of no way to do this in Windows.
You may want to use some IDE with better memory capabilities, rather than the "raw" command interpreter, especially on a platform such as Windows.
Upvotes: 15
Reputation: 21
Unless there is a way of doing it on the activestate shell, no, there is no way to retrieve the exact code you've typed on the shell. At least on Linux, using the Python Shell provided by CPython there is no special way to achieve this. Maybe using iPython.
The func_code attribute is an object representing the function bytecode, the only thing you can get from this object is bytecode itself, not the "original" code.
Upvotes: 0