如何在终端上打印函数的python代码? [英] How to print the python code of a function to the terminal?

查看:78
本文介绍了如何在终端上打印函数的python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,在该系统中,我不断地试验函数的内容,然后运行程序.由于我经常打开许多终端窗口,所以有时我并不完全知道该功能的哪个版本属于哪个终端窗口.

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?

推荐答案

好的.感谢Hoopdady,他在Stackoverflow上提供了类似的问题.只是为了为将来的读者提供解决方案.我需要使用检查模块,并使用其方法"getsource"可以将方法或函数的源代码简单地打印到命令行.

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)

感谢Hoopdady!

Thanks Hoopdady!

这篇关于如何在终端上打印函数的python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆