How to print the python code of a function to the terminal? [duplicate]

你。 提交于 2019-12-23 23:52:34

问题


I've got a system in which I constantly experiment with the contents of a function, after which I run the program. Since I often have many terminal windows open, I sometimes don't exactly know which version of the function belongs to which terminal window.

Is there a way to print the source code of a specific function to the terminal?


回答1:


Alright. Thanks to Hoopdady who provided a similar question here on Stackoverflow. Just to give the solution for future readers. I needed to use the inspect module, and using its method "getsource" I can simply print the source of a method or function to the command line.

import inspect
import mymodule
print inspect.getsource(mymodule.sayHello)

Thanks Hoopdady!



来源:https://stackoverflow.com/questions/15297413/how-to-print-the-python-code-of-a-function-to-the-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!