开发者

Is there any way to see the codes of functions you've typed in your Python CMD

Just found out that when I pressed up in my cmd one of my function codes is no longer accessible meaning I couldn't see the code I made it with. 开发者_StackOverflow社区Is there any way I can see it again? And is there any way I could see all the functions I created for this session in Python cmd?


You can "delete" a function by assigning None to it.

EDIT

If you want to know what you typed, you should probably put your code inside a file and import or execute it. The buffer of the Python interactive interpreter is limited.


dir() should give you everything.

EDIT

>>> def blah():
...   return 1
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'blah']
>>>
>>> dir(blah.func_code)
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__', '__hash__
', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr_
_', '__str__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filenam
e', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_nam
es', 'co_nlocals', 'co_stacksize', 'co_varnames']
>>> blah.func_code.co_code
'd\x01\x00S'


I think your best bet is to do something like:

import readline
readline.write_history_file("myhistory")

in the python shell, where you defined the function.

You can find your function in the "myhistory"-file afterwards.

There exists an inspect module with inspect.getsourcelines, but that only prints code for functions defined in source-files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜